WebView或浏览器通过Scheme调起App

来源:互联网 发布:打的费用计算软件 编辑:程序博客网 时间:2024/05/20 09:22

WebView或浏览器通过Scheme调起App:

一、    在要调起的app中的AndroidManifes.xml文件中,在要被其它应用启动的Activity标签中添加如下拦截器:

<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="me.ricky.sample"                android:path="/userDetails"                android:port="8080"                android:scheme="xieyi" /></intent-filter>

可以只写一个host和scheme,如下:

<data
   
android:scheme="xieyi"

    android:host="me.ricky.sample"/>

 

例:

xieyi://me.ricky.sample/"

具体的配置及详情可见:

http://m.blog.csdn.net/litengit/article/details/74451989

注:引用litengit的文章

 

二、    在html页面中添加:

<a href="xieyi://me.ricky.sample/">打开app</a>

 

例:写一个test.html页面(内容如下,)将此页面在浏览器中打开,点击“打开app”,就会跳转到app中;

test.html:

<!DOCTYPE html>

<html>

<head>

    <meta charset="utf-8">

    <meta name="viewport"content="width=device-width, user-scalable=no"/>

    <linkhref="./css/bootstrap-responsive.css" rel="stylesheet">

    <linkhref="./css/bootstrap.css" rel="stylesheet">

    <style>

div {

    padding:2px;

}

 

    </style>

 

    </script>

</head>

<body>

<div>

<a href="xieyi://me.ricky.sample/">打开app</a>

</div>

</body>

</html>

 

三、    通过webView打开app:

注意:在要打开其它app的应用中设置webVeiw:

private MyWebViewClient mWebViewClient = null;mWebViewClient = new MyWebViewClient();mWebView.setWebViewClient(mWebViewClient);    private class MyWebViewClient extends WebViewClient {        @Override        public boolean shouldOverrideUrlLoading(WebView view, String url) {            if (TextUtils.isEmpty(url)) return false;            // 通过webView打开其它app            try {                if (!url.startsWith("http") || !url.startsWith("https") || !url.startsWith("ftp")) {                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));                    if (isInstall(intent)) {                        getContext().startActivity(intent);                        return true;                    }                }            } catch (Exception e) {                return false;            }            return false;        }        //判断app是否安装        private boolean isInstall(Intent intent) {            return MyApplication.getMyContext().getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY).size() > 0;        }        // MyApplication.getMyContext()应当改成你自己对应的application


具体的配置及详情可见:

http://blog.csdn.net/u012452490/article/details/77931430

注:引用”恢兔子”的文章

 

最佳实践:以上亲测可以使用,我测了20多款浏览器,只有百度浏览器打不开,根据“恢兔子”所说,个人推测可能是百度浏览器屏蔽了此类调用。

原创粉丝点击