Android wakes up the application, one APP wakes up another APP, the A application calls the B application Activity, and the data is transferred between the two APPs

Note: use A as the current application, and B as the application to be woken up

Application B needs to add android:exported="true" to the corresponding Activity in the manifest file

Method 1: wake up with getLaunchIntentForPackage

This method will start the app program to be woken up, which is equivalent to starting the B application and entering its startup page.

"com.test.wakedemo2" is the package name of the application.

//A应用中唤醒部分代码逻辑
Intent intent = getPackageManager().getLaunchIntentForPackage("com.test.wakedemo2");
if (intent != null) {
    //inten可用来在两个APP间传递数据
    intent.putExtra("type", "110");
    //setFlags看自己情况使用,也可以不调用
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}

 Method 2: Wake up with package name + specific Activity name

This method does not start the application program of the B application, but also opens an Activity in the B application in the A application.

//A应用唤醒部分代码逻辑
Intent intent = new Intent(Intent.ACTION_MAIN);
/**知道要跳转应用的包命与目标Activity*/
ComponentName componentName = new ComponentName("com.test.wakedemo2", "com.test.wakedemo2.WakeActivity");
intent.setComponent(componentName);
intent.putExtra("", "");//这里Intent传值
startActivity(intent);

 Method 3: Intent-filter action, wake up with a specific Activity name

 This method also does not start the B application, only opens his Activity

//A应用唤醒部分代码逻辑
Intent intent1 = new Intent("android.intent.action.Wake");
startActivity(intent1);

In the B application, you need to configure the intent-filter for the Activity in the Manifest

<activity android:name=".WakeActivity"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.Wake" />
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</activity>

Method 4: URI wakeup

//A应用唤醒部分代码逻辑
Intent intent2 = new Intent("android.intent.action.Wake", Uri.parse("wake://com.test.demo.demo/WakeActivity?userId=102"));
startActivity(intent2);

The first parameter is the action in the B application, and the second parameter is the Uri. You need to splice the "scheme:// + host + path" in the B application. Finally, if you want to transfer the data, you can splice the data again, as in the above example ?userId=102, you can also not use it or use Intent to pass it.

B application needs to do the following configuration in Manifest

<intent-filter>
    <!-- 指定启动协议 -->
    <data android:host="com.test.demo.demo"
        android:scheme="wake"
        android:path="/WakeActivity"/>

    <action android:name="android.intent.action.Wake" />
    <category android:name="android.intent.category.DEFAULT"/>

</intent-filter>

Guess you like

Origin blog.csdn.net/qq_37980878/article/details/120952599