0、呵呵javascript
在實現這個功能的時候,被不一樣的瀏覽器折磨的胃疼,最後實現了勉強能用,也查考了一下其餘人的博客html
android實現經過瀏覽器點擊連接打開本地應用(APP)並拿到瀏覽器傳遞的數據java
android/iPhone:如何從browser直接打開應用程序或者打開應用商店(若是沒有應用程序)jquery
一、Html頁面(JS不在行,這個是其餘人寫的)android
須要留意的是Android_URL,格式須要符合[scheme]://[host]/[path]?[query]ios
scheme:判別啓動的App。web
host:適當記述瀏覽器
path:傳值時必須的key (沒有也能夠)app
query:獲取值的Key和Value (沒有也能夠)ide
Android_URL = "myapp://www.test.com/openwith?uid=123";
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta content="telephone=no" name="format-detection" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0" /> <title>打開或下載應用</title> <script src="assets/plugins/jquery-1.8.3.min.js" type="text/javascript"></script> </head> <body> <a id="vlink" onClick="try_to_open_app()" style="display:none"></a> <script> var browser={ versions:function(){ var u = navigator.userAgent, app = navigator.appVersion; return { trident: u.indexOf('Trident') > -1, //IE內核 presto: u.indexOf('Presto') > -1, //opera內核 webKit: u.indexOf('AppleWebKit') > -1, //蘋果、谷歌內核 gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐內核 mobile: !!u.match(/AppleWebKit.*Mobile.*/)||!!u.match(/AppleWebKit/), //是否爲移動終端 ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios終端 android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android終端或者uc瀏覽器 iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //是否爲iPhone或者QQHD瀏覽器 iPad: u.indexOf('iPad') > -1, //是否iPad webApp: u.indexOf('Safari') == -1 //是否web應該程序,沒有頭部與底部 }; }() } var iOS_URL = "myapp://www.test.com_uid=123"; var Android_URL = "myapp://www.test.com/openwith?uid=123"; var mtUrl = "http://www.test.com/download"; function open_link() { window.location=mtUrl; } function try_to_open_app() { setTimeout('open_link()', 500); } //IOS if(browser.versions.iPhone){ document.getElementById("vlink").setAttribute("href",iOS_URL); document.getElementById("vlink").click(); } //Android else if(browser.versions.android){ document.getElementById("vlink").setAttribute("href",Android_URL); document.getElementById("vlink").click(); } else{ open_link(); } </script> </body> </html>
二、配置入口
<!-- 默認入口 --> <activity android:name="net.laobanquan.im.splash.StartActivity" android:launchMode="singleTop" android:screenOrientation="portrait" android:theme="@android:style/Theme.Black.NoTitleBar" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- 新建入口 --> <activity android:name="net.test.WebStartActivity" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.VIEW"></action> <category android:name="android.intent.category.DEFAULT"></category> <category android:name="android.intent.category.BROWSABLE"></category> <data android:scheme="myapp" android:host="www.test.com" android:path="/openwith"/> </intent-filter> </activity>
三、Activity入口接受參數
String action = getIntent().getAction(); String uid = null; if(Intent.ACTION_VIEW.equals(action)){ Uri uri = getIntent().getData(); if(uri != null){ uid = uri.getQueryParameter("uid"); } } Log.d(TAG, uid);
四、總結
若是原來點擊圖片的默認入口是StartActivity,那麼最好新建WebStartActivity入口,除了多作「接受參數」其餘和StartActivity同樣