【2025年1月2日】关于腾讯云小程序平台更名为腾讯云超级应用服务的公告
控制台更新动态
Android SDK 更新动态
iOS SDK 更新动态
Flutter 更新动态
IDE 更新动态
基础库更新动态
@JsPlugin(secondary = true);@JsEvent("事件名"),当小程序 js 调用“事件名”时就会调用到@JsEvent修饰的对应方法;@JsEvent 支持定义多个事件名;@JsPlugin(secondary = true)public class CustomPlugin extends BaseJsPlugin {@JsEvent("customAsyncEvent")public void custom(final RequestEvent req) {//获取参数//req.jsonParams//获取当前小程序信息//mMiniAppContext.getMiniAppInfo();//异步返回数据//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//获取当前小程序信息//mMiniAppContext.getMiniAppInfo();//同步返回数据JSONObject jsonObject = new JSONObject();try {jsonObject.put("key", "value");} catch (JSONException e) {throw new RuntimeException(e);}return req.okSync(jsonObject);}/*** 覆盖内置API样例(getAppBaseInfo为SDK内置API)* @param req*/@JsEvent("getAppBaseInfo")public void getAppBaseInfo(final RequestEvent req) {//获取参数//req.jsonParams//获取当前小程序信息//mMiniAppContext.getMiniAppInfo();//异步返回数据//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);}}}
//异步api调用var opts = {api_name: 'customAsyncEvent',progress: function(res) {//sendState状态监听代码},success: function(res) {},fail: function(res) {},complete: function(res) {},data: { // 入参name : 'kka',age : 22}}wx.invokeNativePlugin(opts);//同步api调用var opts = {api_name: 'customSyncEvent',sync:true}var rst = wx.invokeNativePlugin(opts);
@JsEvent定义的事件名称一致;{"extApi": [{"name": "customSyncEvent","sync": true,"params": {"name": "","age": 0,"object": {"key": ""}}},{"name": "customAsyncEvent2","sync": false,"params": {"name": "","age": ""}}],"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;}
{"extApi": [{"name": "customSyncEvent","sync": true,"params": {"name": "","age": ""}},{"name": "customAsyncEvent","sync": false,"params": {"name": "","age": ""}},{"name": "getAppBaseInfo","sync": false,"params": {}}],"overwriteWxApi": true}
var opts = {progress: function(res) {},success: function(res) {},fail: function(res) {},complete: function(res) {}}wx.testState(opts);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() {@Overridepublic boolean doOnActivityResult(int requestCode, int resultCode, Intent data) {TmfMiniSDK.removeActivityResultListener(this);Log.i(ModuleApplet.TAG, data.getStringExtra("key"));req.ok();return true;}});}}
@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() {@Overridepublic 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);}}
文档反馈