文档

Android 小程序发送自定义消息至自定义 View

更新时间:
说明:Android 小程序发送自定义消息至自定义 View 功能仅在 mPaaS 10.1.68.29 及以上版本中支持。当前使用的基线版本低于 10.1.68.29 时,可参考 mPaaS 升级指南 升级基线版本至 10.1.68.29。

创建自定义 View context

  1. this.context = my.createMpaasCustomComponentContext('mpaas-map');

'mpaas-map' 为小程序标签中 id 对应的 value,这里请填入您的 id。

通过 context 向客户端发送消息

  1. this.context.postMessage({
  2. actionType: 'setColor',
  3. data: {
  4. "color": "#FF00FF00"
  5. }
  6. });

其中 actionType 为客户端接收到的消息事件名称,data 为扩展参数。

客户端接收消息并处理原生 View

  1. public class MyTestEmbedView extends MPBaseEmbedView {
  2. ···
  3. @Override
  4. public void onReceivedMessage(String actionType, JSONObject data, H5BridgeContext bridgeContext) {
  5. LoggerFactory.getTraceLogger().debug(TAG, "onReceivedMessage, actionType: " + actionType + ", data: " + data.toString());
  6. if("setColor".equals(actionType)){
  7. mRealView.setColor(Color.parseColor(data.getString("color")));
  8. }
  9. }
  10. ···
  11. }

onReceivedMessage 方法中第一个参数为小程序传入的 actionType,第二个参数为传入的扩展参数,第三个参数为 bridge(可忽略)。

  • 本页导读 (0)
文档反馈