Android uses Scheme to launch an APP from a web page

Keywords: Android Mobile

In my work, I used the H5 interface to call the activity of native s, and I thought of two ways. The first way is through scheme, and the second way is through intercepting the url of webview and redirecting it. Here's how to do this. I hope I can help the students I need.

The first way uses Scheme to invoke a native activity

Using Scheme, you can access a web page with your mobile browser (built-in or third-party), start your own application, or start another application with the WebView.loadUrl() method in one application

Implementation principle:
For Android platform, URI is mainly divided into four parts: scheme, authority,path, queryString.Authority is divided into host and port.The format is as follows:
scheme://host:port/path?qureyParameter=queryString
For instance:
http://www.orangecpp.com:80/tucao?id=hello

In Android's anifest profile, there are configurations in the configuration items that contain:

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

Configuration allows you to filter pages so that pages that match the criteria jump to the application.In general, you only need to set host and schedule.

Implementation Steps
Here's how to implement it
1. Set up test.html under the project asset

<html>
<body>
<h1>Test Scheme</h1>
<!--Manual Tap Jump-->
<a href="myscheme://www.test.com:80/mypath?key=mykey">Click</a>
</body>
</html>

2. Create an Android test project, modify the Manifest file, and add configuration to the Activity you want to receive jumps

 <activity android:name=".SecondActivity">
       <!--Need to add the following intent-filter To configure-->
       <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:scheme="myscheme" />
        </intent-filter>
 </activity>

3. You can get the parameter information of the external jump in the configured Activity.

   WebView webView = (WebView) getActivity().findViewById(R.id.webview);
   webView.loadUrl("file:///android_asset/h/test.html");

This calls up the activity of the native.

The second way to invoke an activity through webView interception redirection

The entire process is implemented in the webView
1. Override the boolean shouldOverrideUrlLoading(WebView view, String url) method of webView
2. Perform specific operations in this method

public boolean shouldOverrideUrlLoading(WebView view, String url) {
      try {
         DebugLog.i(TAG, "shouldOverrideUrlLoading = " + url);
         Uri uriData = Uri.parse(url);  //Convert intercepted url to uri
         String scheme = uriData.getScheme();// scheme to get uri
         if ("Agreed scheme".equals(scheme)) { // Jump if this scheme was agreed upon before native and H5
             // Jump to the specified activity
          } else { // If not continue in H5
               view.loadUrl(url);
          }
         }catch (Exception e){
          e.printStackTrace();
        }
         return true;
      }

That's it

Posted by kee1108 on Mon, 17 Jun 2019 10:22:05 -0700