The following is the realization of App jump applet in Android and return to App page (RN call jump wechat applet) :
First, Android native first integrated wechat SDK
Integrated address: developers.weixin.qq.com/doc/oplatfo…
1. Put the downloaded SDK into liBS and integrate according to the document
2. Start code integration
Start by creating the WXEntryActivity class
. publicclass WXEntryActivity extends WXCallbackActivity implements IWXAPIEventHandler {
private IWXAPI api;
privare String WEIXIN_APPID = "XXXX";// AppID in wechat open platform
protected void onSetContent(Bundle bundle) {
/ / register API
api = WXAPIFactory.createWXAPI(this, WEIXIN_APPID);
api.handleIntent(getIntent(), this);
}
@Override
public void onReq(BaseReq baseReq) {
System.out.print(baseReq);
}
@Override
public void onResp(BaseResp resp) {
if(resp.getType() == ConstantsAPI.COMMAND_LAUNCH_WX_MINIPROGRAM) {
WXLaunchMiniProgram.Resp launchMiniProResp = (WXLaunchMiniProgram.Resp) resp;
String extraData =launchMiniProResp.extMsg;
} finish(); }}Copy the code
Androidmanifest.xml Settings
<activity
android:name="... WXEntryActivity"
android:taskAffinity="Name of your bag" // This is also important
android:launchMode="singleTask" // This must be a singleTask, otherwise the applet cannot return the App
android:exported="true"
android:theme="@style/dialog_custom"
android:windowSoftInputMode="stateHidden"
>
</activity>
Copy the code
RN in the call App jump small program method
. publicclass LaunchWxAppModule extends ReactContextBaseJavaModule {
public static final String REACTCLASSNAME = "Come up with a name for yourself.";
private Context mContext;
public LaunchWxAppModule(ReactApplicationContext reactContext) {
super(reactContext);
mContext = reactContext;
}
public static boolean isWeixinAvilible(Context context) {
final PackageManager packageManager = context.getPackageManager();/ / get packagemanager
List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// Get package information for all installed programs
if(pinfo ! =null) {
for (int i = 0; i < pinfo.size(); i++) {
String pn = pinfo.get(i).packageName;
if (pn.equals("com.tencent.mm")) {
return true; }}}return false;
}
@Override
public String getName() {
return REACTCLASSNAME;
}
@ReactMethod
public void startMiniProgram(String ghId,String miniPagepath,String version,Callback callback){
if(! isWeixinAvilible(mContext)){ Alert.showShortToast("Please install wechat client first");
return;
}
String appId = "Your wechat open platform AppId"; AppId = AppId; // AppId = AppId
IWXAPI api = WXAPIFactory.createWXAPI(mContext, appId);
WXLaunchMiniProgram.Req req = new WXLaunchMiniProgram.Req();
req.userName = ghId; // Fill in the applets with the original id gh_
if(miniPagepath! ="") {
req.path = miniPagepath; // Pull up the applet page of the parameter path, do not default to pull up the applet home page
}
if (version.equals("debug")){
req.miniprogramType = WXLaunchMiniProgram.Req.MINIPROGRAM_TYPE_TEST;
}else if(version.equals("preview")){
req.miniprogramType = WXLaunchMiniProgram.Req.MINIPROGRAM_TYPE_PREVIEW;
}else {
req.miniprogramType = WXLaunchMiniProgram.Req.MINIPTOGRAM_TYPE_RELEASE;// Open development, trial and official versions
}
api.sendReq(req);
// callback.invoke(req);}}Copy the code
call
const LaunchWxApp = require('react-native').NativeModules.REACTCLASSNAME;//REACTCLASSNAME is the name you wrote in the code above
// The parameters are: applet original ID, applet jump path (default ""), version (e.g., "debug")
MpNativeModule.startMiniProgram(ghId,miniPagepath,verision,() = >{});Copy the code
Two, wechat small program to achieve the return App
The official link: developers.weixin.qq.com/miniprogram…
Wechat native use:
<button open-type="launchApp" app-parameter="wechat" binderror="launchAppError"> open the APP < / button >Copy the code
Mine was written by Taro:
<Button
openType="launchApp"
appParameter="wechat"
onLaunchapp={(res) = > {
console.log("Successful callback", res);
}}
onError={(error) = > {
console.log("Wrong.", error); }} > Open APP </Button>Copy the code
The important thing to note here is that if you find that the callback succeeds but does not return to the App, you need to look at the App integration to see if there is any error.