What are Deep Links?
Let’s understand this with the help of a situation. If you happened to click a link on a webpage and suddenly it opens an application inside you phone or opens a dialog asking you to choose an application to handle this click. Yes, this is called deep linking.
You can read more about deep links from here.
You can do the same with your application too.
Lets see how we can do this.
Step 1
Add the below intent filters in your Android Manifest inside the activity that need to be opened when a user clicks a link in the browser.
<activity android:name=".MainActivity"> <intent-filter android:label="@string/filter_view_http_gizmos"> <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://www.example.com/gizmos” --> <data android:host="example.com" android:pathPrefix="/" android:scheme="http" /> <!-- note that the leading "/" is required for pathPrefix--> </intent-filter> <intent-filter android:label="@string/filter_view_example_gizmos"> <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 "example://gizmos” --> <data android:host="gizmos" android:scheme="example" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
String Resources
Below are the string resource for this example.
<resources> <string name="app_name">test1</string> <string name="filter_view_example_gizmos">GIZMOS</string> <string name="filter_view_http_gizmos">GIZMOS HTTP</string> </resources>
Handle Deep Link Click
We accept the date in the incoming link from getIntent() and intent.getData(). Take a look at the implementation. Its that easy.
import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = getIntent(); if (null != intent) { String action = intent.getAction(); Uri data = intent.getData(); Log.i("Main", "Data :: " + data); } } }
Testing Deep Links
Go to Terminal or Command Prompt :
Go to platform-tools and find adb if you have not added adb in your global variables
adb shell am start -W -a android.intent.action.VIEW -d "example://gizmos" com.example.android
If you app package is different from your package folder, you can issue command like this.
./adb shell am start -W -a android.intent.action.VIEW -d example://gizmos "com.coderzheaven.testapp/demo.coderzheaven.com.test1.MainActivity"
Hope you enjoy this post. Please share your thoughts below.