Android evokes APP from the web

foreword

When Zhihu is opened in the mobile phone browser, there will be a button to open in the app. Click to open it directly and jump to the details page. Isn’t it a bit magical, how is it done?

Effect preview

Uri Scheme

Configure intent-filter

AndroidManifest.xml

<activity android:name=".MainActivity">
    <!-- 需要添加下面的intent-filter配置 -->
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data
            android:host="myhost"
            android:path="/main"
            android:port="1024"
            android:scheme="myscheme" />
    </intent-filter>
</activity>

test page

Create a new assets file under main, and write a simple Html web page for WebView display for testing.

index.html:

<html>
<head>
    <meta charset="UTF-8">
</head>
<body>
<h1>这是一个 WebView</h1>

<a href="market://details?id=com.tencent.mm">open app with market</a>
<br/>
<br/>

<a href="myscheme://myhost:1024/main?key1=value1&key2=value2">open app with Uri Scheme</a>

<br/>
<br/>


</body>
</html>

Web View loading:

webView.loadUrl("file:///android_asset/index.html");

target page

Accept the parameters and do the corresponding processing.

Intent intent = getIntent();
if (null != intent && null != intent.getData()) {
    // uri 就相当于 web 页面中的链接
    Uri uri = intent.getData();
    Log.e(TAG, "uri=" +uri);
    String scheme = uri.getScheme();
    String host = uri.getHost();
    int port = uri.getPort();
    String path = uri.getPath();
    String key1 = uri.getQueryParameter("key1");
    String key2 = uri.getQueryParameter("key2");
    Log.e(TAG, "scheme=" + scheme + ",host=" + host
            + ",port=" + port + ",path=" + path
            + ",query=" + uri.getQuery()
            + ",key1=" + key1 + ",key2=" + key2);
}

The print message is as follows:

uri=myscheme://myhost:1024/main?key1=value1&key2=value2
scheme=myscheme,host=myhost,port=1024,path=/main,query=key1=value1&key2=value2,key1=value1,key2=value2

principle

myscheme://myhost:1024/main?key1=value1&key2=value2, through a link, why can the corresponding APP be started? The implementation and principle of Web evoking Android app , the article mentioned that the key code is in the shouldOverrideUrlLoading method of the native browser of Android 6.0, and the core is implemented in the UrlHandler class. code show as below:

final static String SCHEME_WTAI = "wtai://wp/";
final static String SCHEME_WTAI_MC = "wtai://wp/mc;";
boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
    if (view.isPrivateBrowsingEnabled()) {
        // Don't allow urls to leave the browser app when in
        // private browsing mode
        return false;
    }
    if (url.startsWith(SCHEME_WTAI)) {
        // wtai://wp/mc;number
        // number=string(phone-number)
        if (url.startsWith(SCHEME_WTAI_MC)) {
            Intent intent = new Intent(Intent.ACTION_VIEW,
                    Uri.parse(WebView.SCHEME_TEL +
                            url.substring(SCHEME_WTAI_MC.length())));
            mActivity.startActivity(intent);
            // before leaving BrowserActivity, close the empty child tab.
            // If a new tab is created through JavaScript open to load this
            // url, we would like to close it as we will load this url in a
            // different Activity.
            mController.closeEmptyTab();
            return true;
        }
        //……
    }

source code

The public account "Student Wu Xiaolong" replied "SchemeSample" to get a complete example of this exercise.

Deep Links

As shown in the figure, before Android M, if you click a link and there are multiple APPs, a dialog box will pop up asking the user which application to use to open - including the browser application. Google has implemented an auto-verify mechanism on Android M that allows developers to avoid this pop-up box, allowing users to jump directly to their app without having to select a list.

create

Detailed explanation of App Links implementation of Android M

Android M App Links: Implementation, bugs, and workarounds

I didn't verify it, because I couldn't play it. I could update the content of Deep Links and set up a local server myself.

disadvantages

Requires Android M

Requires Android 6.0 (minSdkVersion level 23) and higher to use.

.well-known/assetlinks.json

The developer must maintain a website associated with the app that declares the relationship between your website and your intent filters by hosting a digital asset link JSON file at:

https://domain.name/.well-known/assetlinks.json

refer to

Android uses Scheme to start APP from a web page

What is Deep Link

Deep Linking Technology Android Mobile Developers Must Know

Handling Android App Links

No public

My public number: Wu Xiaolong, welcome to communicate~

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325430861&siteId=291194637