Anthony
Anthony

Reputation: 36008

Intercepting a url to launch application

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>

Upvotes: 0

Views: 354

Answers (1)

David S.
David S.

Reputation: 6705

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.

Upvotes: 1

Related Questions