JS唤醒Android APP(包括在外部浏览器和WebView)

问题:当用户在手机浏览器中点击一个按钮时,如果手机上已经安装该应用,则直接打开,如果没有安装,则转向应用下载页面。
1.AndroiManifest.xml中配置
请在App启动的Activity的那个节点中加入

    <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:host="webawakeapp.com"
                    android:pathPrefix="/test/"
                    android:scheme="webawakeapp" />
            </intent-filter>

data中的host和scheme可以自定义

2.在浏览器中唤醒APP
需要在第一个启动的Activity中的onCreate中加入

          Intent intent = getIntent();
          Uri uri = intent.getData();
        if (uri != null) {     
        String pid = uri.getQueryParameter("pid");  
        }

pid为自定义链接的参数,如果仅仅只是唤醒APP,就可以忽略此步骤

3.在WebView中的唤醒
需要在WebView中配置

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

拿到了参数,剩下的就不用我说了

4.最重要的是在html中的配置

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>模拟点击Web跳转到App</title>
    <script type="text/javascript">
    function show(info){
        document.getElementById("shows").innerHTML = info;
    }


    </script>
</head>
<body>
<b>模拟点击Web跳转到App可以将js与java写在两个app中进行测试以达到web测试唤醒拟定app的效果</b>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<a href="webawakeapp://webawakeapp.com/test/scheme?name=google09&page=1">模拟点击Web跳转到App </a><br/><br/>
</body>
</html>

OK,就这么简单

发布了22 篇原创文章 · 获赞 31 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_36158551/article/details/83687634