美文网首页
微信小程序 - 打开APP

微信小程序 - 打开APP

作者: IT姑凉 | 来源:发表于2019-12-26 21:11 被阅读0次

    接上一篇文章 --- APP分享小程序到微信(cordova-plugin-wechat)

    一、官方小程序打开APP使用方法

    小程序端

    需要将 button 组件 open-type 的值设置为 launchApp。如果需要在打开 APP 时向 APP 传递参数,可以设置 app-parameter 为要传递的参数。通过 binderror 可以监听打开 APP 的错误事件。

    app 端

    APP 需要接入 OpenSDK。 文档请参考 iOS / Android

    Android 第三方 app 需要处理 ShowMessageFromWX.req 的微信回调,iOS 则需要将 appId 添加到第三方 app 工程所属的 plist 文件 URL types 字段。 app-parameter 的获取方法,请参考 Android SDKSample 中 WXEntryActivity 中的 onResp 方法以及 iOS SDKSample 中 WXApiDelegate 中的 onResp 方法。

    详见:小程序打开APP官方说明

    二、测试小程序打开APP

    小程序端对应界面修改

    wxml:

    <button 
       open-type="launchApp" 
       app-parameter="wechat" 
       binderror="launchAppError">
       测试小程序打开APP
    </button>
    

    js:

    Page({
      launchAppError (e) {
        console.log(e.detail.errMsg)
      }
    })
    

    如果APP进程在,可以唤起APP,如果APP kill掉进程,唤不起

    1、修改res/values/strings.xml文件:

    <resources>
        // 添加
        <style name="NoActionBarFullScreen" parent="android:style/Theme.Holo.NoActionBar.Fullscreen">
            <item name="android:windowBackground">@android:color/white</item>
        </style>
        <style name="transparentDialog" parent="android:style/Theme.Holo.Light.NoActionBar.Fullscreen">
            <item name="android:windowBackground">@android:color/transparent</item>
            <item name="android:windowFullscreen">false</item>
        </style>
    </resources>
    

    2、修改AndroidManifest.xml文件:

    <activity
    android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" 
    android:label="@string/activity_name"
    android:launchMode="singleTop" 
    android:name="MainActivity"
    android:theme="@style/NoActionBarFullScreen" //添加
    android:windowSoftInputMode="adjustResize">
       <intent-filter android:label="@string/launcher_name">
          <action android:name="android.intent.action.MAIN" />
          <category android:name="android.intent.category.LAUNCHER" />
       </intent-filter>
    </activity>
    
    <activity 
    android:exported="true"
    android:label="@string/launcher_name"
    android:launchMode="singleTask"
    android:name=".wxapi.WXEntryActivity"
    android:taskAffinity="xxx.xx.com.cn"
    android:theme="@style/transparentDialog" //添加
    >
       <intent-filter>
          <action android:name="android.intent.action.VIEW" />
          <category android:name="android.intent.category.DEFAULT" />
          <data android:scheme="wx111111111111111" />
       </intent-filter>
    </activity>
    

    可以唤起APP,现在是要实现从小程序的分享卡片打开小程序后,要获得小程序传过来的参数

    借鉴

    // WXEntryActivity.java
    @Override
     
    public void onReq(BaseReq req) {
     
        if(req.getType() == ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX){
     
            ShowMessageFromWX.Req showReq = (ShowMessageFromWX.Req) req;
     
            WXMediaMessage wxMsg = showReq.message;
     
            WXAppExtendObject obj = (WXAppExtendObject) wxMsg.mediaObject;
     
            String extInfo = obj.extInfo;// 对应 小程序 app_paramter 参数
     
        }
     
    }
    

    IOS

    - (void)onReq:(BaseReq *)req {
        // 需要判断类型
        LaunchFromWXReq *request = (LaunchFromWXReq *)req;
        ((LaunchFromWXReq*)req).message.messageExt
        // 对应 小程序 app_paramter 参数
    }
    
    - (void)onReq:(BaseReq *)req {
        WXMediaMessage *msg = req.message;
        //从微信启动App
        NSString *strMsg = [NSString stringWithFormat:@"openID: %@, messageExt:%@", req.openID, msg.messageExt];
        NSLog(@"%@", strMsg);
    }
    

    相关文章

      网友评论

          本文标题:微信小程序 - 打开APP

          本文链接:https://www.haomeiwen.com/subject/yxckoctx.html