Call the local android of android app program code through the Html web page

  • 2020-05-19 05:52:02
  • OfStack

1. Open Android local app via the html page

1. Start by writing a simple html page


<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <title>Insert title here</title>
    </head>
    <body>
        <a href="m://my.com/"> Open the app</a><br/>
    </body>
</html>

2. Configuration of local app in Android


 in AndroidManifest In the list file intent-filte Add the following elements: 
 <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="my.com" 
                    android:scheme="m" />
</intent-filter>

The sample screenshot is as follows:

Then use the "mobile browser" or "webview" to open the local html page. Click "open APP" to successfully open the local app page

2. How to obtain the data brought by the web page through this method

It doesn't make any sense to just open it, but the most important thing is, we're going to pass the data, so how are we going to pass the data?

We can use the above method to send some data to local app. First, we will change the webpage 1 and modify the code:


<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Insert title here</title>
    </head>
    <body>
        <a href="m://my.com/?arg0=0&arg1=1"> Open the app</a><br/>
    </body>
</html>

(1). If you open this page through a browser, the way to get the data is as follows:


Uri uri = getIntent().getData();  String test1= uri.getQueryParameter("arg0");  String test2= uri.getQueryParameter("arg1");

(2) if you use webview to access the webpage, the operation to obtain data is as follows:


webView.setWebViewClient(new WebViewClient(){
  @Override
  public boolean shouldOverrideUrlLoading(WebView view, String url) {
      Uri uri=Uri.parse(url);
          if(uri.getScheme().equals("m")&&uri.getHost().equals("my.com")){
              String arg0=uri.getQueryParameter("arg0");
              String arg1=uri.getQueryParameter("arg1");

          }else{
              view.loadUrl(url);
          }
      return true;
  }
});


Related articles: