點擊瀏覽器中的URL鏈接,啟動特定的App。
首先做成HTML的頁面,頁面內容格式如下:
<a href="[scheme]://[host]/[path]?[query]">啟動應用程序</a>
這一句就可以了。
各個項目含義如下所示:
scheme:判別啟動的App。 ※詳細后述
host:適當記述
path:傳值時必須的key ※沒有也可以
query:獲取值的Key和Value ※沒有也可以
作為測試好好寫了一下,如下:
<a href="myapp://jp.app/openwith?name=zhangsan&age=26">啟動應用程序</a>
接下來是Android端。
首先在AndroidManifest.xml的MAIN Activity下追加以下內容。(啟動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:scheme="myapp" android:host="jp.app" android:pathPrefix="/openwith"/> </intent-filter>
HTML記述的內容加入<data …/>。
其中必須的內容僅scheme,沒有其他內容app也能啟動。
※注意事項:intent-filter的內容【android.intent.action.MAIN】和 【android.intent.category.LAUNCHER】這2個,不能與這次追加的內容混合。
所以,如果加入了同一個Activity,請按以下這樣做,否則會導致應用圖標在桌面消失等問題。
<intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <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="myapp" android:host="jp.app" android:pathPrefix="/openwith"/> </intent-filter>
這樣的話,沒有問題。
接下來在Activity中需要取值的地方添加以下代碼,我是直接寫在OnCreate函數里的:
Intent i_getvalue = getIntent();
String action = i_getvalue.getAction();
if(Intent.ACTION_VIEW.equals(action)){
Uri uri = i_getvalue.getData();
if(uri != null){
String name = uri.getQueryParameter("name");
String age= uri.getQueryParameter("age");
}
}
這樣就能獲取到URL傳遞過來的值了。