1. 程式人生 > >通過URL開啟APP

通過URL開啟APP

如果想要從網頁中開啟App,只要在AndroidManifest.xml檔案中設定Activity的相關屬性則可,具體如下:

<activity

    android:name=".StartActivity">


    <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.BROWSABLE" /><data android:scheme="weixin" android:host="gameforums" android:pathPrefix="/message"/> </intent-filter>


 </activity>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

這裡想要注意的是,如果這個Activity同時也是App的啟動頁面的話,intent-filter不能同時放在一塊。

接下來就是在StartActivity裡面獲取資料了,獲取傳遞過來的引數程式碼如下:

Intent intent = getIntent();

String scheme = intent.getScheme();
Log.i(TAG, "scheme:" + scheme);

if (uri != null) {

    String host = uri.getHost();
    Log.i(TAG
, "host:"+host);
 String dataString = intent.getDataString();
 Log.i(TAG, "dataString:" + dataString);
 String param2 = uri.getQueryParameter("param2");
 Log.i(TAG, "param2:" + param2);
 String path = uri.getPath(); Log.i(TAG, "path:" + path); String encodedPath = uri.getEncodedPath(); Log.i(TAG, "encodedPath:" + encodedPath); String queryString = uri.getQuery(); 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

接下來是測試,寫一個簡單的html來進行測試:

<a href="weixin://gameforums/message?param=456">開啟App</a>
  • 1

測試結果如下圖: 
這裡寫圖片描述