Enable deep links for App content in Android, you need to fist add intent filters for the relevant activities in your application manifest. The intent filters allow deep linking to the content in any of your activities.

Add Intent Filters for Links

For example, a user clicks on a website link http://stacktips.com on browser, that will resolve the link and open the content in installed activity.

<activity
    android:name="com.example.MainActivity"
    android:label="@string/my_app" >
    <intent-filter android:label="@string/my_app">
	
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
		
        <!-- Accepts URIs that begin with "http://stacktips.com/tutorials” -->
        <data android:scheme="http" android:host="stacktips.com" android:pathPrefix="/tutorials" />        
    </intent-filter>
</activity>

Once the above intent filters are added, Android will be able to route any Intent that has matching URIs to your app at runtime.

Read Data from Incoming Intents

Once Android system starts the routed activity through an intent filter. You can get the data bundle from Intent to determine what you which activity or fragment to render. Call the getData() and getAction() methods to retrieve the data and action associated with the incoming Intent. You can call these methods at any time during the lifecycle of the activity, but you should generally do so during early callbacks such as onCreate() or onStart().

The following code snippet shows how to retrieve data from an Intent

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Intent intent = getIntent();
    String action = intent.getAction();
    Uri data = intent.getData();
}

Test Your Deep Links

You can use the Android Debug Bridge with the activity manager tool to test that the intent filter URIs you specified for deep linking resolve to the correct app activity. You can run the adb command against a device or an emulator.

The following command tries to view a target app activity that is associated with the specified URI.

$ adb shell am start -W -a android.intent.action.VIEW -d http://stacktips.com/tutorials/android com.example

Nilanchala

A blogger, a bit of tech freak and a software developer. He is a thought leader in the fusion of design and mobile technologies. He is the author of Xamarin Mobile Application Development for Android Book (goo.gl/qUZ0XV3), DZone MVB and founder of stacktips.com.

Discussions

Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.