[Android] How to enable the “deep linking” for app content?

Sometimes, we need to provide a way to enter an Android application from web browser. In that case, you can send the intent by a hyperlink on the web page. When user clicks the hyperlink, browser will try to send the intent on Android system. Then, the system will switch to application from web browser and the application also receives the content from browser that sent.

HOW to do that?
First, you need to know the intent-based URI. Here is the URI format,
intent:/
#Intent;
    scheme=[string];
    package=[string];
    action=[string];
    category=[string];
    component=[string];
    [data_type].[key]=[value];
end
If you want to append the extra data in the URI, you need to notice the type of data. Here is the list of data-type,

String (S)
Boolean (B)
Byte (b)
Character (c)
Double (b)
Float (f)
Integer (i)
Short (s)
Long (l)

We generate the hyperlink contains the URI on the web. A html example is by following,

<html>
<head>
    <title>launch application from browser</title>
    <meta http-equiv = "Content-Type" content = "text/html; charset=utf-8">
</head>
<body>
<p>
<a href="intent:/#Intent;scheme=test_scheme;package=com.test.application;S.string_extra=data;end">Click Me!</a>
</p>
</body>
</html>

In the app, you can append the intent-filter to catch the intent.

<intent-filter>
    <data android:scheme="test_scheme">
    <action android:name="android.intent.action.VIEW">
    <category android:name="android.intent.category.DEFAULT">
    <category android:name="android.intent.category.BROWSABLE">
</intent-filter>

Because the security issue, we add the BROWSABLE category to prevent intent sender is not web browser.

and here is the example for getting the intent’s extra data,

Intent intent = getIntent();
if (intent != null) {
    if (intent.getAction().contentEquals(Intent.ACTION_VIEW)) {
        String userId = intent.getStringExtra(“key”);
        // process another operations
    }
}

Reference:

發佈留言