Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 249
Shudy Avatar asked Oct 25 '17 10:10

Shudy


People also ask

Can you deep link to another app?

In the context of mobile apps, deep linking consists of using a uniform resource identifier (URI) that links to a specific location within a mobile app rather than simply launching the app. Deferred deep linking allows users to deep link to content even if the app is not already installed.

How do I open an app using deep link?

First, you specify the BROWSABLE category so your deep link can work in a browser. Without it, clicking the deep link on a browser won't resolve to your app. Second, the DEFAULT category lets your app handle implicit intents. If it's missing, the intent must specify the app component name for the activity to start.

What is deeplink redirect?

Deep links send mobile device users directly to relevant pages in your app rather than your website. Users click on ads and go directly to your app pages. You can use deep links in many Google Ads products, including App campaigns for engagement, App dynamic remarketing, and Search, Shopping, and Display campaigns.

What is deep linking routing?

Traditional deep links route users to app content as long as the app is already installed when the link is opened. Deferred - pass context through install. Deferred deep links can route users to content even if the app is not installed when the link is opened.


3 Answers

The above answer can only open the app with screen defined as LAUNCHER, not with deep link.

This will work to link app XYZ with any app:

 private void startAppXYZfromThisFuckinApp() {
   // pass the uri (scheme & screen path) of a screen defined from app XXX that you want to open (e.g HomeActivity)
   Uri uri = Uri.parse("xyz://screen/home");
   Intent mapIntent = new Intent(Intent.ACTION_VIEW, uri);

  //Verify if app XYZ has this screen path
    PackageManager packageManager = getPackageManager();
    List<ResolveInfo> activities = 
    packageManager.queryIntentActivities(mapIntent, 0);
    boolean isIntentSafe = activities.size() > 0;

   //Start HomeActivity of app XYZ because it's existed
    if (isIntentSafe) {
        startActivity(mapIntent);
    }
 }

And obviously, in app XYZ AndroidManifest.xml must be something like this:

<activity
 android:name=".HomeActivity"
 <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:host="screen/home"
   android:scheme="xyz" />
 </intent-filter>
   

It will now open screen HomeActivity from app XYZ!

like image 139
lehongphucit Avatar answered Oct 21 '22 08:10

lehongphucit


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);
    }
like image 40
David M. Avatar answered Oct 21 '22 08:10

David M.


Change your manifest like this

<data
    android:host="open"
    android:pathPattern="/myAction?param=123"
    android:scheme=" myApp" />

To send an intent in the first activity

Intent intent = new Intent (Intent.ActionView);
intent.setData (Uri.Parse (DEEP_LINK_URL));

And in your second activity

if(getIntent()!=null){
    Intent deepLink = getIntent();
    deepLink.getScheme();
    deepLink.getData().getPath();   
}
like image 45
Sharath kumar Avatar answered Oct 21 '22 06:10

Sharath kumar