@JsPlugin(secondary = true)public class CustomPlugin extends BaseJsPlugin {@JsEvent("customAsyncEvent")public void custom(final RequestEvent req) {//获取参数//req.jsonParams//异步返回数据//req.fail();//req.ok();JSONObject jsonObject = new JSONObject();try {jsonObject.put("key", "test");} catch (JSONException e) {e.printStackTrace();}req.ok(jsonObject);}@JsEvent("customSyncEvent")public String custom1(final RequestEvent req) {//获取参数//req.jsonParams//同步返回数据JSONObject jsonObject = new JSONObject();try {jsonObject.put("key", "value");} catch (JSONException e) {throw new RuntimeException(e);}return req.okSync(jsonObject);}/*** 测试覆盖系统API* @param req*/@JsEvent("getAppBaseInfo")public void getAppBaseInfo(final RequestEvent req) {//获取参数//req.jsonParams//异步返回数据//req.fail();//req.ok();JSONObject jsonObject = new JSONObject();try {jsonObject.put("key", "test");} catch (JSONException e) {e.printStackTrace();}req.ok(jsonObject);}@JsEvent("testState") public void testState(final RequestEvent req) { try { //回调中间状态 req.sendState(req, new JSONObject().put("progress", 1)); req.sendState(req, new JSONObject().put("progress", 30)); req.sendState(req, new JSONObject().put("progress", 60)); req.sendState(req, new JSONObject().put("progress", 100)); } catch (JSONException e) { e.printStackTrace(); } JSONObject jsonObject = new JSONObject(); try { jsonObject.put("key", "test"); req.ok(jsonObject); } catch (JSONException e) { throw new RuntimeException(e); } }}
{"extApi": [{//事件名,与“创建API”示例中@JsEvent定义的事件保持一致"name": "customSyncEvent",//是否同步调用,与“创建API”示例数据返回方式保持一致"sync": true,//定义参数//a.json格式可以嵌套//b.字符串参数value设置为""即可//c.数字参数value设置为0即可"params": {"name": "","age": 0,"object": {"key": "",}}}],//true:当自定义的API事件名与小程序SDK内置的方法名,会覆盖SDK内置的//API,最终调用会调用的开发者自定义的API中"overwriteWxApi": false}
@ProxyService(proxy = MiniAppProxy.class)public class MiniAppProxyImpl extends BaseMiniAppProxyImpl {@Overridepublic MiniConfigData configData(Context context, int configType, JSONObject params) {if(configType == MiniConfigData.TYPE_CUSTOM_JSAPI) {//自定义JsApi配置MiniConfigData.CustomJsApiConfig customJsApiConfig = new MiniConfigData.CustomJsApiConfig();customJsApiConfig.jsApiConfigPath = "tcmpp/custom-config.json";return new MiniConfigData.Builder().customJsApiConfig(customJsApiConfig).build();}return null;}
wx.
时,会自动弹出展示小程序系统内置的 API 接口,如下图所示:{"extApi": [{"name": "customSyncEvent","sync": true,"params": {"name": "","age": ""}},{"name": "customAsyncEvent","sync": false,"params": {"name": "","age": ""}},{"name": "getAppBaseInfo","sync": false,"params": {}}"overwriteWxApi": true}
wx.customAsyncEvent({"name":"123","age":"18"})wx.getAppBaseInfo()//会覆盖系统API,然后调用到自定义API中
@JsPlugin(secondary = true) public class CustomPlugin extends BaseJsPlugin {@JsEvent("testStartActivityForResult") public void testStartActivityForResult(final RequestEvent req) { Activity activity = req.activityRef.get(); TmfMiniSDK.addActivityResultListener(new IActivityResultListener() { @Override public boolean doOnActivityResult(int requestCode, int resultCode, Intent data) { TmfMiniSDK.removeActivityResultListener(this); Log.i(ModuleApplet.TAG, data.getStringExtra("key")); req.ok(); return true; } }); //注意:requestCode必须>=1000000,否则可能与内部requestCode冲突,引起未知问题 activity.startActivityForResult(new Intent(activity, TransActivity.class), 1000000); }}
TmfMiniSDK.addActivityResultListener()TmfMiniSDK.removeActivityResultListener()
//注意:requestCode必须>=1000000,否则可能与内部requestCode冲突,引起未知问题 activity.startActivityForResult(new Intent(activity, Xxxx.class), 1000000);
<activity android:name=".activity.TransActivity" android:exported="false" android:screenOrientation="portrait" android:theme="@style/TransparentTheme"/>
public class TransActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.applet_activity_tran); /*************************业务开发*************************///业务开发完成后返回数据并调用finishIntent intent = new Intent(); intent.putExtra("key", "value"); setResult(RESULT_OK, intent); finish(); }}
@JsPlugin(secondary = true) public class CustomPlugin extends BaseJsPlugin {@JsEvent("testStartActivityForResult") public void testStartActivityForResult(final RequestEvent req) { Activity activity = req.activityRef.get(); TmfMiniSDK.addActivityResultListener(new IActivityResultListener() { @Override public boolean doOnActivityResult(int requestCode, int resultCode, Intent data) { TmfMiniSDK.removeActivityResultListener(this); Log.i(ModuleApplet.TAG, data.getStringExtra("key")); req.ok(); return true; } }); //注意:requestCode必须>=1000000,否则可能与内部requestCode冲突,引起未知问题 activity.startActivityForResult(new Intent(activity, TransActivity.class), 1000000); }}
本页内容是否解决了您的问题?