tencent cloud

Feedback

Multi-Process Interaction

Last updated: 2024-11-21 17:28:19
    The mini program SDK creates a separate process for each mini program, isolating it from the host application's process.
    The SDK provides a unified API for communication between the mini program process and the host process.

    Check if current process is a mini program process

    API description:
    /**
     * Whether the current process is a mini program process
     * @param context
     * @
     */
    public static boolean isMiniProcess(Context context)

    Host process calls mini program process plugin

    1. To implement a sub-process plugin extension

    Implementation steps:
    Inherit BaseIpcPlugin.
    Annotate the class with @IpcMiniPlugin.
    Annotate the invoke method with @IpcEvent and define the plugin event name.
    Sample code:
    @IpcMiniPlugin
    public class MiniProcessIpcPlugin extends BaseIpcPlugin {
    public static final String EVENT = "MiniProcessPlugin";
    
    @Override
    @IpcEvent(EVENT)
    public void invoke(IpcRequestEvent req) {
    
    String value = req.data.getString("key");
    
    Log.d(ModuleApplet.TAG, "current process:" + ProcessUtil.getProcessName(req.context) + "|call from main process: " + value);
    Bundle resp = new Bundle();
    resp.putString("key", "i am ok");
    req.ok(resp);
    }
    }

    2. Plug-ins that call mini program process in main process

    API description
    /**
     * Call a mini-program process plugin for mini program introduction, and this method can only be called from the main process
     *
     ///@param appId Mini program ID
     * @param eventId
     * @param request
     * @param callback
     */
    public static void callMiniProcessPlugin(String appId, String eventId, Bundle request, IpcCallback callback)
     
    /**
     * Call a mini program process plugin for develop/preview mini program, and this method can only be called from the main process
     *
     ///@param appId Mini program ID
     * @param appVerType Mini program type. See MiniApp
     * @param eventId
     * @param request
     * @param callback
     */
    public static void callMiniProcessPlugin(String appId, int appVerType, String eventId, Bundle request, IpcCallback callback)
    Sample code:
    TmfMiniSDK.callMiniProcessPlugin("", MiniProcessIpcPlugin.EVENT, bundle, new IpcCallback() {
    @Override
    public void result(boolean isSucc, Bundle response) {
    Log.d(ModuleApplet.TAG, "current process:" + ProcessUtil.getProcessName(ModuleApplet.sApp) + "|isSucc:" + isSucc + "|callback data: " + response.getString("key"));
    }
    });

    Calling host process plugin in mini program process

    1. To implement a host process plugin extension

    Implementation steps
    Inherit BaseIpcPlugin.
    Annotate the class with @IpcMainPlugin.
    Annotate the invoke method with @IpcEvent and define the plugin event name.
    Sample code:
    @IpcMainPlugin
    public class MainProcessIpcPlugin extends BaseIpcPlugin {
    public static final String EVENT = "MainProcessPlugin";
    
    @Override
    @IpcEvent(EVENT)
    public void invoke(IpcRequestEvent req) {
    String value = req.data.getString("key");
    
    Log.d(ModuleApplet.TAG, "current process|" + ProcessUtil.getProcessName(req.context) + "|call from mini process:" + value);
    Bundle resp = new Bundle();
    resp.putString("key", "i am ok");
    req.ok(resp);
    }
    }

    2. Calling host process plugin in mini program process

    API description
    Parameters
    eventId: Indicates event name defined by host process
    request: Indicates parameters passed from mini program process to host process plugin
    ipcCallBack: Indicates callback to receive the host process
    /**
     * Calls the main process Plugin, and this method can only be called in the process of mini process
    *
    * @param eventId
    * @param request
    * @param callback
    */
    public static void callMainProcessPlugin(String eventId, Bundle request, IpcCallback callback)
    Sample code:
    TmfMiniSDK.callMainProcessPlugin(MainProcessIpcPlugin.EVENT, bundle, new IpcCallback() {
    @Override
    public void result(boolean isSucc, Bundle response) {
    Log.d(ModuleApplet.TAG, "current process:" + ProcessUtil.getProcessName(ModuleApplet.sApp) + "|isSucc:" + isSucc + "|callback data:" + response.getString("key"));
    }
    });
    
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support