Web page opens Android APP

Analytical principle

In terms of the Android platform, URI divided into three parts:
scheme, authority, path
which authority is divided into host and port. Format is as follows:

<scheme>://<host>:<port>[<path>|<pathPrefix>|<pathPattern>]

Corresponding to the manifest <data>configured as follows:

<data android:host=""
      android:mimeType=""
      android:path=""
      android:pathPattern=""
      android:pathPrefix=""
      android:port=""
      android:scheme=""
      android:ssp=""
      android:sspPattern=""
      android:sspPrefix=""/>

Where the scheme is to be parameters, if not specified, other attributes that are invalid!
If the host is not specified, port, path, pathPrefix, pathPattern are invalid!

Our most commonly used scheme, host, port, pathfour configurations.

Implementation

First, AndroidManifestin MainActivityadding one <intent-filter>:

<intent-filter>  
      <action android:name="android.intent.action.VIEW" />  
      <category android:name="android.intent.category.BROWSABLE" />  
      <category android:name="android.intent.category.DEFAULT"/>  
      <data android:scheme="protocol" android:host="domain" android:pathPrefix="/link" />  
  </intent-filter>  

Then add a link in your web page:

<a href="protocol://domain/link>打开app</a>

Finally, click on a link, if the app is successfully ejected, then congratulations, you succeeded.

expand

Open the app may not be enough light, sometimes we have to transfer data, how to pass data to it?

We can use the above method, some data to the app, then link to change it:

<a href="protocol://domain/link?id=123>打开app并传递id</a>

Then add code on MainActivity onCreate method in the app:

Uri uri = getIntent().getData();  
String id= uri.getQueryParameter("id");  

This allows it to transfer data!

If the webview within the application, the data acquisition operations of:

webView.setWebViewClient(new WebViewClient(){
  @Override
  public boolean shouldOverrideUrlLoading(WebView view, String url) {
    Uri uri=Uri.parse(url);
      if(uri.getScheme().equals("protocol")&&uri.getHost().equals("domain")){
        String id = uri.getQueryParameter("id");
          }else{
              view.loadUrl(url);
          }
        return true;
  }
});

API

getScheme(); //获得Scheme名称 

getDataString(); //获得Uri全部路径 

getHost(); //获得host

Attach the official api uri link
https: //developer.android.com ...

Comments welcome

Guess you like

Origin www.cnblogs.com/homehtml/p/12505749.html