Jump to the specified Activity of the other app
Published: 2021-04-03 09:23:49

Recently, the company has a demand to click on the link of H5 page or the button of the APP(I named A here) to go directly to a specified activity of other app(named B). After research, I summarized some methods.

-----------------------------App B--------------------------------

First of all, For app B,in the AndroidManifest.xml You can configure it by adding an intent filter to the corresponding activity, as shown below
```
<activity android:name=".SecondActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
                <data android:scheme="hello" />
            </intent-filter>
        </activity>
```

Next, add the relevant test code to the corresponding activity, as follows:
```
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        Intent intent = getIntent();
        String action = intent.getAction();
        if (Intent.ACTION_VIEW.equals(action)) {
            Uri uri = intent.getData();
            if (uri != null) {
                String host = uri.getHost();
                String dataString = intent.getDataString();
                String id = uri.getQueryParameter("id");
                String path = uri.getPath();
                String path1 = uri.getEncodedPath();
                String queryString = uri.getQuery();
                Log.d("Stone", "host:" + host);
                Log.d("Stone", "dataString:" + dataString);
                Log.d("Stone", "id:" + id);
                Log.d("Stone", "path:" + path);
                Log.d("Stone", "path1:" + path1);
                Log.d("Stone", "queryString:" + queryString);
            }
        }
    }
```

Finally, the log is as follows:
```
host:schemedemo
dataString:hello://schemedemo/get/info?id=001
id:001
path:/get/info
path1:/get/info
queryString:id=001
```

-------------------------------App A------------------------------
Go straight to the code.
**1. jump to ther app
```
//luanch App
    public static void launchApp(Context context,String packageNAME) {
        //Is the app installed or not
        if (isAppInstalled(context,packageNAME)){
            //if have installed,open app
            startActivityXXX(params...);
        }else{
            //If not, go into the system appstore to find this app and prompt you to download the app
            goToMarket(context, packageNAME);
        }
    }

    //Enter the app store and download the corresponding app。
    private static void goToMarket(Context context, String packageName) {
        Uri uri = Uri.parse("market://details?id=" + packageName);
        Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
        try {
            context.startActivity(goToMarket);
        } catch (Exception e) {
        }
    }
    //Determine whether the app has been installed
    private static boolean isAppInstalled(Context context, String packageName) {
        try {
            context.getPackageManager().getPackageInfo(packageName,0);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
```
**2. luch other app outside this app
```
public static void LunchAppByUri(Context context, String url) {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addCategory(Intent.CATEGORY_DEFAULT);//filter condition
        context.startActivity(intent); 
    }
```
or You can also change the form like this
```
public static void LunchAppByUri(Context context, String url){
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_DEFAULT);//filter condition
        intent.setData(Uri.parse(url));
        context.startActivity(intent);
    }
```

------------------------------ H5 --------------------------------
this part is easy:
```
<a href="paraches://schemedemo/get/info?id=10000">open android app</a>
```

------------------------ jump to B inside A ----------------------
```
    public static void lunchAppInside(Context context,String packageName,String classNme){
        Intent intent = new Intent(Intent.ACTION_MAIN);
//such as :"com.mogujie.B", "com.mogujie.B.SecondActivity
        ComponentName componentName = new ComponentName(packageName, classNme);
        intent.setComponent(componentName);
        context.startActivity(intent);
    }
```
Please note that：
For app B,in the AndroidManifest.xml，you need add One more attribute —— android:exported="true"，finally like this:
```
<activity android:name=".SecondActivity" android:exported="true"/>
```