Search code examples
androidandroid-intentdeep-linking

Launch application from another using a deeplink


I am working on two apps A and B, I wish to interlink them with Deep links.

App B has a deeplink like the following: myApp://open/myAction?param=123

It looks like:

<!-- Update myAction deep link -->
<intent-filter android:label="@string/launcherName">

    <action android:name="android.intent.action.VIEW" />

    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE />
    <data
        android:host="open/*"
        android:scheme="myApp" />
</intent-filter>

If I launch the app with the adb it works perfect.

Now I'm trying to launch application B, when user clicks a button within Activity A.

I tried this (found in: GoogleDeveloper ) when the button is clicked (OnClickListener)

// Build the intent
Uri myAction = Uri.parse(mEditText.getText().ToString()); // is something like: `myApp://open/myAction?param=1AD231XAs`
Intent mapIntent = new Intent(Intent.ACTION_VIEW, myAction);

// Verify it resolves
PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(mapIntent, 0);
boolean isIntentSafe = activities.size() > 0;

// Start an activity if it's safe
if (isIntentSafe) {
    startActivity(mapIntent);
}

Yet I cannot open the other app with this app.


Solution

  • Try to create the Intent from PackageManager and set the action (ACTION_VIEW) and the data (myAction) before launching deepLink:

        Uri myAction = Uri.parse(mEditText.getText().toString());
    
        PackageManager packageManager = getPackageManager();
        Intent intent = packageManager.getLaunchIntentForPackage(<app_destination_package>);
    
        if (intent != null) {
            intent.setAction(Intent.ACTION_VIEW);
            intent.setData(myAction);
            startActivity(intent);
        }