Search code examples
androidandroid-activity

Set app activity to accept action.VIEW action


When user tap on button this code is happen:

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

And it's runs OK, open website in default web browser. But I am trying to display the menu, that appears if you have more applications capable to display web pages installed. So I prepared blank application FakeBrowser with this AndroidManifest file:

<?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>

I try to set the second intent-filter tag, so my app will be accept this browser request, but it's still immediately opens default browser. Please can you tell me, what I have wrong? Thanks.


Solution

  • You have to use an IntentChooser:

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

    More info: http://developer.android.com/training/basics/intents/sending.html

    UPDATE: And on your manifest:

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