0

I am trying to intercept a url so that my application opens up when user accesses the URL. I am following this answer in a related question https://stackoverflow.com/a/2958870

I've added the following in AndroidManifest.xml

   <activity
        android:name="com.myapp.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <data android:scheme="http" android:host="myapp.com"/>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

And have the following in onCreate of MainActivity.java

Uri data = getIntent().getData();
//String scheme = data.getScheme(); 
//String host = data.getHost(); 
if (data == null)
    Log.d("Data is null", "");
else
    Log.d("Data is not null", "");

When I launch the app in the emulator I am noticing that the "Data is null" debug message is coming. I had to comment out the scheme and host because that was causing my application to fail on load time and the reason for I guess was because data was null.

Am I missing something or doing something wrong?

Edit: I've tried splitting the intent-filter like this:

    <activity
        android:name="com.myapp.MainActivity"
        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>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:host="myapp.com" android:scheme="http" />
        </intent-filter>
    </activity>

Also, when I just have one intent-filter (below) my app does not even launch on the emulator when I press run in android studio.

    <activity
        android:name="com.myapp.MainActivity"
        android:label="@string/app_name" >
        <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="myapp.com" android:scheme="http" />
        </intent-filter>
    </activity>
Community
  • 1
  • 1
Anthony
  • 27,490
  • 32
  • 129
  • 238
  • 1
    Try splitting your intent filter in two. One for the `MAIN` action with `LAUNCHER` category, one for the `VIEW` action with scheme/host. – Geobits Dec 10 '13 at 20:28

1 Answers1

1

Try this intent filter instead:

<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="myapp.com" android:scheme="http" />
</intent-filter>

This is an implicit intent, but I tried it and it worked for me. I think you need the android.intent.category.BROWSABLE category.

David S.
  • 6,043
  • 1
  • 23
  • 44
  • Please see my edit. that didn't work for me. When I just have the intent you suggested, my app does not even launch on the emulator when I press `run` – Anthony Dec 10 '13 at 21:08
  • That's because it's an implicit intent. Email yourself a link like http://myapp.com/something and click the link in the message. – David S. Dec 10 '13 at 21:11
  • Ah, I see. Yes, that works. However, I'd still like the app to be launched when I press `run` in IDE so I added `MAIN` and `LAUNCHER` back. Thanks – Anthony Dec 10 '13 at 21:37