获取小程序栈堆和当前信息

您可以按照以下步骤,获取小程序栈堆和当前信息。

  1. 创建 CustomApiBridgeExtension 类继承 SimpleBridgeExtension 类。代码示例如下:

    public class CustomApiBridgeExtension extends SimpleBridgeExtension {
    
        private static final String TAG = "CustomApiBridgeExtension";
    
        @ActionFilter
        public void tinyToNative(@BindingId String id,
                                 @BindingNode(App.class) App app,
                                 @BindingNode(Page.class) Page page,
                                 @BindingApiContext ApiContext apiContext,
                                 @BindingExecutor(ExecutorType.UI) Executor executor,
                                 @BindingRequest JSONObject params,
                                 @BindingParam("param1") String param1,
                                 @BindingParam("param2") String param2,
                                 @BindingCallback BridgeCallback callback) {
            RVLogger.d(TAG, "id: " + id +
                    "\napp: " + app.toString() +
                    "\npage: " + page.toString() +
                    "\napiContext: " + apiContext.toString() +
                    "\nexecutor: " + executor.toString());
            RVLogger.d(TAG, JSONUtils.toString(params));
    
            JSONObject result = BridgeResponse.SUCCESS.get();
    
            // 将结果返回给小程序
    
            Stack stack = MriverApp.getAppStack();
            Enumeration enumerationLists = stack.elements();
    
    
            JSONArray jsonArray = new JSONArray();
            while (enumerationLists.hasMoreElements()) {
                JSONObject jsonObject = new JSONObject();
                MRApp o = (MRApp) enumerationLists.nextElement();
                jsonObject.put("AppId", o.getAppId());
                jsonObject.put("AppVersion", o.getAppVersion());
                jsonArray.add(jsonObject);
            }
            String tinyappStr = jsonArray.toJSONString();
            // result.put("message", "客户端接收到参数:" + param1 + ", " + param2 + "\n返回 Demo 当前包名:" + apiContext.getActivity().getPackageName());
            result.put("message", tinyappStr);
            callback.sendJSONResponse(result);
        }
    
    }
    
  2. 在启动小程序之前行注册。代码示例如下:

    MriverEngine.registerBridge(CustomApiBridgeExtension.class);
  3. 启动小程序。代码示例如下:

    Mriver.startApp(activity, "2021042620210426");

阿里云首页 移动开发平台 mPaaS 相关技术圈