Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a deep linking uri in Android?

For example, there is an uri example://activityone?foo=bar which can open up an application and launch one of the activity by this adb command

adb shell am start -W -a android.intent.action.VIEW -d "example://activityone?foo=bar" com.example.deeplinking

Are there any other ways to launch the android app through this uri (example://activityone?foo=bar)? How can I put this uri in an email, and when it is clicked, it will launch the app?

like image 897
s-hunter Avatar asked Dec 11 '22 16:12

s-hunter


1 Answers

First, you should read the documentation for this: Intents and Intent Filters. Specifically the section "Receiving an Implicit Intent".

As others have stated, using a custom scheme for this has issues.

  • They aren't always treated as links
  • You don't own them like you own a domain name (host).

So you should define an activity in you Manifest with an intent filter for your host and use a real scheme. See the "Action Test", "Category Test" and "Data Test" sections of the Intent & Intent Filters documentation to see how to configure this for your specific use case.

<activity android:name="com.example.app.ActivityOne">
    <intent-filter>
        <data android:scheme="http"/>
        <data android:host="example.com"/>
        <data android:path="/activityone"/>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
    </intent-filter>
</activity>

Then you will be able to use a link like the following.

http://example.com/activityone?foo=bar

Since this is an http link the system will ask the user to choose which app to open it with (your app or their preferred browser). If they choose your app it will start ActivityOne which can get the query data like so.

public class ActivityOne extends Activity {

    @Override
    public void onResume() {
        super.onResume();
        Intent intent = getIntent();
        Uri uri = intent.getData();
        String foo = uri.getQueryParameter("foo");
    }

}

If you use a custom scheme in the intent filter then you will (most likely) be the only app registered to handle that intent and the user will not have to select your app. However, it will be much harder for you to include a link with a custom scheme in an email. Most email clients will not recognize anything with a custom scheme as a link and it will not be clickable.

like image 150
mpkuth Avatar answered Jan 15 '23 02:01

mpkuth