2009-02-08 64 views
146

我希望我的意圖在用戶訪問某個網址時啓動:例如,Android市場可以通過http://market.android.com/這個網址進行操作。 youtube也一樣。我也希望我也這樣做。Android響應URL Intent

+8

那裏http://stackoverflow.com/questions/2448213/how-to-implement-my-very-own-uri-schema-on-android/2448531#2448531 – neu242 2010-06-24 06:11:29

+2

是一個很好的回答這個問題有一個更好的答案這個問題http://stackoverflow.com/questions/1609573/intercepting-links-from-the-browser-to-open-my-android-app – rds 2012-09-16 15:05:22

回答

183

我做到了!使用<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:host="www.youtube.com" android:scheme="http" /> 
</intent-filter> 

這很好用!

7

您可能需要爲您的意圖過濾器添加不同的排列,以使其在不同情況下(http/https/ect)可以工作。

例如,我不得不做以下的應用程序,當用戶打開一個鏈接到谷歌驅動形式這將打開,www.docs.google.com/forms

注意路徑前綴是可選的。

 <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="http" 
       android:host="docs.google.com" 
       android:pathPrefix="/forms"/> 
      <data 
       android:scheme="http" 
       android:host="www.docs.google.com" 
       android:pathPrefix="/forms" /> 

      <data 
       android:scheme="https" 
       android:host="www.docs.google.com" 
       android:pathPrefix="/forms" /> 

      <data 
       android:scheme="https" 
       android:host="docs.google.com" 
       android:pathPrefix="/forms" /> 
     </intent-filter>