How to start the activity of another APP from one APP

Posted by ChrisBoden on Sat, 02 May 2020 08:53:50 +0200

How to start the Activity of another APP from one APP

1. Start with custom action

In this way, you only need to set an action in the code, and the system will automatically filter to find the corresponding Activity of this action

Code of current APP

Intent intent = new Intent();
//Here is the custom action used
intent.setAction("transBundle.app");
startActivity(intent);

Configuration of activity of APP to be started in Android manifest.xml

<!- Corresponding customization needs to be configured action->
<activity
        android:name=".MyActivity"
        android:label="@string/app_name" 
        android:exported="true">
        <intent-filter>
            <action android:name="transBundle.app"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>

2. Find by specifying the package name and class name in Intent

Write the following code directly in the current APP to open the activity of the specified APP

ComponentName componetName = new ComponentName(  
            "com.poynt.weibo",  //This is the package name of another application  
            "com.poynt.weibo.ui.IndexActivity");   //This parameter is the full pathname of the Activity to start

        try {  
            Intent intent = new Intent();  
            intent.setComponent(componetName);  
            startActivity(intent);  
        } catch (Exception e) {  
            Toast.makeText(getApplicationContext(), "You can prompt the user here that the application is not found, or do other operations!", 0).show();  
        }

3. Start with scheme

In fact, this method is similar to method 1, except that the scheme parameter is added. Scheme is more used to start our app on the web page or H5. For example, we can directly open our app through scheme on the official mobile phone website. Here, we just start another app with scheme from the app

Code of current application:

Uri uri = Uri.parse("app://my.test");
Intent intent = new Intent("transBundle.app", uri);
startActivity(intent);

Android manifest configuration of APP to be opened

 <activity
        android:name=".MyActivity"
        android:label="service" 
        android:exported="true">

        <intent-filter>
            <action android:name="transBundle.app"/>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:scheme="app" android:host="my.test"/>
        </intent-filter>
    </activity>

app is similar to http://www.baidu.com http in represents the transmission protocol; my.test is similar to www.baidu.com, representing the host name

Topics: Android xml Mobile