说明:Android 小程序发送自定义消息至自定义 View 功能仅在 mPaaS 10.1.68.29 及以上版本中支持。当前使用的基线版本低于 10.1.68.29 时,可参考 mPaaS 升级指南 升级基线版本至 10.1.68.29。
创建自定义 View context
this.context = my.createMpaasCustomComponentContext('mpaas-map');
'mpaas-map' 为小程序标签中 id 对应的 value,这里请填入您的 id。
通过 context 向客户端发送消息
this.context.postMessage({actionType: 'setColor',data: {"color": "#FF00FF00"}});
其中 actionType 为客户端接收到的消息事件名称,data 为扩展参数。
客户端接收消息并处理原生 View
public class MyTestEmbedView extends MPBaseEmbedView {···@Overridepublic void onReceivedMessage(String actionType, JSONObject data, H5BridgeContext bridgeContext) {LoggerFactory.getTraceLogger().debug(TAG, "onReceivedMessage, actionType: " + actionType + ", data: " + data.toString());if("setColor".equals(actionType)){mRealView.setColor(Color.parseColor(data.getString("color")));}}···}
onReceivedMessage 方法中第一个参数为小程序传入的 actionType,第二个参数为传入的扩展参数,第三个参数为 bridge(可忽略)。
该文章对您有帮助吗?