Sunday, August 7, 2016

How to Enable Deep Links On Android

What Are Deep Links?

Android deep links open a specific page within an app and optionally pass data to it. Developers may find deep links particularly useful for actions, such as clicking a notification or sending an app link via email.

Let's take an email client as an example. When the user clicks the notification of an email she received, it opens a deep link that takes her to the email in the app. Last but not least, deep links also allow Google to index your app and link to specific sections of your app in searches. The deep link appears as a search result in Google and can take the user to a particular section of your app.

Implementing Deep Links

To add a deep link to your app, you must add it to your android manifest file as an intent filter. Take a look at the following example.

  1. <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name"
  2.     android:supportsRtl="true" android:theme="@style/AppTheme">
  3.     <activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar">
  4.         <intent-filter>
  5.             <!-- Notice that the MAIN activity already has an intent-filter. This is not
  6.             A deep link because its action is not a VIEW-->
  7.             <action android:name="android.intent.action.MAIN" />
  8.             <category android:name="android.intent.category.LAUNCHER" />
  9.         </intent-filter>
  10.     </activity>
  11.     <activity android:name="com.example.matthew.deeplinks.LinkActivity" android:label="@string/title_activity_link"
  12.         android:theme="@style/AppTheme.NoActionBar">
  13.         <intent-filter>
  14.             <!-- Sets the intent action to view the activity -->
  15.             <action android:name="android.intent.action.VIEW" />
  16.             <!-- Allows the link to be opened from a web browser -->
  17.             <category android:name="android.intent.category.BROWSABLE" />
  18.             <!-- Allows the deep link to be used without specifying the app name -->
  19.             <category android:name="android.intent.category.DEFAULT" />
  20.             <!-- URI tutsplus://deeplink -->
  21.             <data android:scheme="tutsplus" android:host="deeplink"/>
  22.             <!-- URI http://www.mydeeplink.com -->
  23.             <data android:scheme="http" android:host="www.mydeeplink.com"/>
  24.         </intent-filter>
  25.     </activity>
  26. </application>

The <action> and <data> tags are required. The <action> tag chooses what happens in the app when the link is clicked. The <data> tag specifies what URIs are acceptable as deep links to the page.

In the above example, navigating to either http://www.mydeeplink.com  takes the user to the LinkActivity activity. The <category> tags specify the properties of the deep link. Notice that you need to create a separate intent filter  for each URI scheme and each activity.

You can create multiple links to the same activity. To differentiate these, you need to parse the intent's data in your code to differentiate the links. This is usually done in the onCreate() method by reading in the data and acting accordingly.

  1. protected void onCreate(Bundle savedInstanceState) {
  2.         super.onCreate(savedInstanceState);
  3.         setContentView(R.layout.activity_link);
  4.         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  5.         setSupportActionBar(toolbar);
  6.  
  7.         Intent in = getIntent();
  8.         Uri data = in.getData();
  9.         // Do something with data. For example, open certain email in view.
  10.     }

Testing Deep Links

Android Studio makes it very easy to test deep links. Click Run > Edit Configurations to edit the configuration of the project.



Open the General tab at the top and enter the URI in the Deep Link field in the Launch Options section. When you launch your app using Android Studio, it will attempt to open the specified URI.

Conclusion

Now that you know how to create and use deep links, you can open up new entry points for users to interact with your app. Users may use Google search on their phones to find pages within your app and you can create notifications that open a specific page in your app when clicked.
Written by Matthew Kim

If you found this post interesting, follow and support us.
Suggest for you:

The Complete Android & Java Course - Build 21 Android Apps

Android Application Programming - Build 20+ Android Apps

The Complete Android Developer Course: Beginner To Advanced!

Android: From Beginner to Paid Professional

The Complete Android Developer Course - Build 14 Apps


No comments:

Post a Comment