12

I am trying to figure out how to start an app from a URL, and how I should write that URL.

I have the following code in my AndroidManifest:

<activity android:name=".MyActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"></action>
        <action android:name="android.intent.action.VIEW"></action>
        <category android:name="android.intent.category.LAUNCHER"></category>
        <category android:name="android.intent.category.DEFAULT"></category>
        <category android:name="android.intent.category.BROWSABLE"></category>
        <data android:host="my.app" android:scheme="http"></data>
    </intent-filter>
</activity>

I used a URL as explained in this answer, but nothing happens.

Please let me know if my intent is well written, and how I should write the URL that calls that app, and note that I need to call my "Main" Activity.

Community
  • 1
  • 1
FelipeDev.-
  • 3,113
  • 3
  • 22
  • 32

1 Answers1

27

You need to have two <intent-filter> elements for this <activity>. One will be for MAIN and LAUNCHER. The other will be for VIEW, BROWSABLE/DEFAULT, and your <data> element:

<activity android:name=".MyActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"></action>
        <category android:name="android.intent.category.LAUNCHER"></category>
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.VIEW"></action>
        <category android:name="android.intent.category.DEFAULT"></category>
        <category android:name="android.intent.category.BROWSABLE"></category>
        <data android:host="my.app" android:scheme="http"></data>
    </intent-filter>
</activity>

Then, http://my.app should launch your activity.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • thx for your comment, but this doesn't work for me. I tried writing that URL in the browser, but my app doesn't start. – FelipeDev.- Sep 14 '11 at 14:58
  • @FelipeDev: Here is a sample project demonstrating this technique: https://github.com/commonsguy/cw-advandroid/tree/master/Introspection/URLHandler – CommonsWare Sep 14 '11 at 15:01
  • @FelipeDev: and here is a URL to a page that you can visit in your browser that invokes links that will launch that application: http://commonsware.com/sample – CommonsWare Sep 14 '11 at 15:03
  • thx a lot! It takes me a couple of minutes trying to find aout the URL trick. I mean, I can have a page with the link to my app and if I click it, my app get started, but I cant copy/paste the same link in the browser to try open the app. Anyway, your answer solve my problem. Thx man! – FelipeDev.- Sep 14 '11 at 16:31
  • In my case, this works in my HTC Butterfly's browser, and works in Firefox for android, but didn't work on Chrome for android... – RRTW Jul 22 '13 at 05:33
  • 2
    Something I've found is that if the url you're using has a path component, but your intent filter doesn't contain an android:path section, it won't be picked up. – Adam Aug 22 '13 at 03:52
  • 1
    Ok and if someone knows how to launch an app in separate sandbox? When I launch my app from browser in starts on top of the browser. I need something like launchig apps from Play Market on Open button click, but with url links – Defuera May 29 '14 at 11:20
  • It starts within the same app from which it was opened ? is that a bug ? – divyenduz Apr 30 '15 at 13:20
  • When I'm using two intent filters, two separate instances of app gets created, one when I click on icon to launch the app, and one when I click the url. How do I prevent that from happening. – user1801879 May 20 '15 at 17:02