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:
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
public static void callMiniProcessPlugin(String appId, String eventId, Bundle request, IpcCallback 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
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"));
}
});
Was this page helpful?