配置参考:
APP拉起小程序
private void launchMiniProgram(String path) {
String appId = "你的appid"; // 填应用AppId
IWXAPI api = WXAPIFactory.createWXAPI(PinDanActivity.this, appId);
WXLaunchMiniProgram.Req req = new WXLaunchMiniProgram.Req();
req.userName = "原始id"; // 填小程序原始id
req.path = path; //拉起小程序页面的可带参路径,不填默认拉起小程序首页
req.miniprogramType = WXLaunchMiniProgram.Req.MINIPROGRAM_TYPE_PREVIEW;// 可选打开 开发版,体验版和正式版
api.sendReq(req);
}
- req.userName 参数是小程序
原始id
,不是小程序的appid,不然会提示跳转失败。 - req.path 参数是小程序里面的页面路径,及json文件里面的路径,比如:/pages/index/index?id=1
- req.miniprogramType 小程序的版本
回调WXEntryActivity
@Override
public void onResp(BaseResp baseResp) {
// baseresp.getType 1:第三方授权, 2:分享
LogUtil.i("baseresp.getType = " + baseResp.getType());
switch (baseResp.getType()) {
case 1:
break;
case 2:
break;
case ConstantsAPI.COMMAND_LAUNCH_WX_MINIPROGRAM:
WXLaunchMiniProgram.Resp launchMiniProResp = (WXLaunchMiniProgram.Resp) baseResp;
//获取参数
String extraData = launchMiniProResp.extMsg; //对应小程序组件 <button open-type="launchApp"> 中的 app-parameter 属性
break;
}
}
APP分享小程序
private void shareMiniProgram(String url) {
WXMiniProgramObject miniProgramObj = new WXMiniProgramObject();
miniProgramObj.webpageUrl = url; // 兼容低版本的网页链接
miniProgramObj.miniprogramType = WXMiniProgramObject.MINIPROGRAM_TYPE_PREVIEW;// 正式版:0,测试版:1,体验版:2
miniProgramObj.userName = "原始id"; // 小程序原始id
miniProgramObj.path = "/pages/home/home"; //小程序页面路径
WXMediaMessage msg = new WXMediaMessage(miniProgramObj);
msg.title ="分享的标题"; // 小程序消息title
msg.description = "分享的描述"; // 小程序消息desc
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pindan);
msg.thumbData = bmpToByteArray(bitmap, true); // 小程序消息封面图片,小于128k
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("webpage");
req.message = msg;
req.scene = SendMessageToWX.Req.WXSceneSession; // 目前支持会话
api.sendReq(req);
}
小程序打开APP
<button open-type="launchApp" app-parameter="wechat" binderror="launchAppError">打开APP</button>
- open-type=“launchApp” 开放能力
- app-parameter=“wechat” 传参
- binderror=“launchAppError” 失败回调
Page({
launchAppError (e) {
console.log(e.detail.errMsg)
}
})