2014-09-10 24 views
0

當用戶輕按按鈕,這個代碼是發生:設置應用程序的活動,接受action.VIEW行動

Uri web = Uri.parse("http://www.google.com"); 
Intent i = new Intent(Intent.ACTION_VIEW,web); 
startActivity(i); 

而且它運行在默認Web瀏覽器確定,打開網站。但是我試圖顯示菜單,如果您有更多的應用程序能夠顯示已安裝的網頁,就會顯示該菜單。 所以我準備了空白的應用FakeBrowser這個AndroidManifest文件:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.pavel.fakebrowser" > 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".MyActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 

     <intent-filter> 
      <data android:scheme="http" android:host="google.com"/> 
      <category android:name="android.intent.category.BROWSABLE" /> 
      <action android:name="android.intent.action.VIEW" /> 
     </intent-filter> 
    </activity> 
</application> 

我嘗試設置第二意圖過濾器標籤,所以我的應用程序會接受這個瀏覽器請求,但它仍然會立即打開默認瀏覽器。 請你能告訴我,我有什麼錯? 謝謝。

回答

1

你必須使用一個IntentChooser:

Uri web = Uri.parse("http://www.google.com"); 
Intent i = new Intent(Intent.ACTION_VIEW,web); 
startActivity(Intent.createChooser(i, "Choose Program")); 

更多信息:http://developer.android.com/training/basics/intents/sending.html

UPDATE: 並在您的清單:

 <intent-filter> 
      <action android:name="android.intent.action.VIEW" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
      <data android:scheme="http"/> 
     </intent-filter> 
+0

是的,這部分失蹤了。但結果仍然相同。當我點擊按鈕時,沒有任何選擇對話框,頁面在默認瀏覽器中打開。 所以我仍然在FakeBrowser應用程序的AndroidManifest.Xml中有一些錯誤? – 2014-09-10 18:03:02

+0

我編輯了答案。讓我知道它是否適合你 – 2014-09-10 18:34:41

+0

太棒了,現在它可以工作,非常感謝 – 2014-09-10 18:53:56