2

I'm trying to create a schema like myapp://somthing/anotherthing that if a webpage or any other app link to that schema will open my app.

I've added this to my AndroidManifest.xml for my main activity:

    <activity
        android:name="com.example.MainActivity"
        android:label="@string/title_activity_main"
        android:configChanges="orientation"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Light.NoTitleBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="myapp" />
        </intent-filter>
    </activity>

but when I click a link in a webpage that's linking to myapp://somthing/anotherthing it just gives me a page not found type of error and does not open my app.

Everywhere I look, I see that I need to add the <data> tag inside <intent-filter>. What is missing here?

developer82
  • 11,911
  • 18
  • 76
  • 134
  • From what I read in the [docs](http://developer.android.com/reference/android/content/IntentFilter.htm) it sounds like the filter needs to have path, type and scheme specified. Where type looks like MIME type. Perhaps try adding an authority (server) or path prefix? – Diederik Jun 02 '15 at 20:07
  • This is a possible duplicate, see [this question](http://stackoverflow.com/questions/2958701/launch-custom-android-application-from-android-browser) – mmark Jun 02 '15 at 20:07
  • @Diederik the link you provided doesn't work. Also, I've tried specifying the host - it doesn't work. – developer82 Jun 03 '15 at 02:53
  • @mmark as I mentioned I already searched and tried various suggested answers - including that "duplicate" one - I wouldn't write that I have a problem if I haven't. – developer82 Jun 03 '15 at 02:56
  • try http://developer.android.com/reference/android/content/IntentFilter.html – Diederik Jun 03 '15 at 05:56

1 Answers1

2

Searching for examples in other related questions i couldn't find one using both <action android:name="android.intent.action.MAIN" /> and <action android:name="android.intent.action.VIEW" /> (the latter seems to be mandatory for this kind of behaviour).

Anyway, it is possible to use both. This is my working example:

<activity
        android:name="com.example.MainActivity"
        android:label="@string/title_activity_main"
        android:configChanges="orientation"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Light.NoTitleBar" android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.LAUNCHER" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data android:scheme="myapp" />
            </intent-filter>
        </activity>

So, the following tags seem to do the trick:

android:exported="true"
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

This is the anchor i'm using from the web page:

<a href="myapp://somthing/anotherthing">Open the app!</a>

UPDATE: As developer82 mentioned in the comments, if you do this the app icon will dissapear and the user won't be able to open the app. To fix this, you'll need to create a new tag to put the <data> along with default, browsable categories and view action.

The <activity> tag should look like this:

    <activity
            android:name="com.example.MainActivity"
            android:label="@string/title_activity_main"
            android:configChanges="orientation"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Light.NoTitleBar" android:exported="true">
            <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:scheme="myapp" />
            </intent-filter>
            </activity>

Let me know if it helped!

mmark
  • 1,176
  • 11
  • 19
  • Another side effect to doing such thing is that having the tag in the main activity makes the icon to not appear in drawer - so the user can't actually start the app. What I did was to create a dummy activity that will respond to my schema and open the activity I need from the data coming in to that activity. – developer82 Jun 05 '15 at 06:29
  • @developer82 that's correct. However, according to [this](http://stackoverflow.com/a/30623546/3641308) answer, it can be fixed by adding a new tag. There's no need for you to use a dummy activity. I edited my answer with a tested example. Let me know if it helps – mmark Jun 05 '15 at 14:39
  • What were the major changes you made? **category.DEFAULT** and **category.BROWSABLE**? – IgorGanapolsky Aug 15 '17 at 17:25