1

I'm attempting to launch my app automatically when visiting a URL (test.com) in this example

however every time I do so I am asked which application I should use to do so. I'd like a way to launch my app every time without prompting for user interaction.

I've done research into how this can be done and have found the following:

Android Eliminate Complete Action Using dialog

But if I implement an explicit intent instead of an implicit intent - how do I specify to launch my activity when visiting test.com since I wont be using

<data android:scheme="http" android:host="test.com" android:pathPrefix="/" />

IMPLICIT INTENT (works successfully - launches app when visiting test.com but prompts user to select app - which is undesired - need to launch my app without the complete action using prompt)

<activity android:name="com.nfc.linked.Warning">
    <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:scheme="http" android:host="test.com" android:pathPrefix="/" />
    </intent-filter>
</activity>

EXPLICIT INTENT (ATTEMPT):

public class Warning extends Activity {

    Intent intent = new Intent(this, Warning.class);
    startActivity(intent); 

}
Community
  • 1
  • 1
NoobNinja
  • 123
  • 2
  • 14

1 Answers1

4

You can't.

What you call an Explicit Intent targets exactly one class.

What you've done earlier is to create an intent-filter in your manifest, which listens for Intents related to a URL along the lines of http://*.test.com/*. However, since HTTP is a very common protocol, and your URI is simply a website, other apps like browsers which listen for URLs like http://* will also offer to show the content. Due to this, Android creates a chooser, from which the user must select your app.

The only way around this is to have a button which opens the chooser for http://test.com in your app, ask the user to press it and choose your app as the default handler for such URLs. It cannot be done programmatically for security reasons, as then malicious apps can set themselves as the default for various actions.

Another option would be to come up with your own URI scheme, like Google Play's market://. As your app will be the only one using this, it will be the only one available to handle the link. However, you will need to make sure all third parties trying to open your app through another place (like a link on a webpage or from another app) follow your URL scheme. Additionally, if a user presses one of the third party links and your app is not installed, it will likely result in an ActivityNotFoundException on the user's device.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • I'm not sure either implementation will work for what I need... I'm trying to create an application which redirects to a block screen when a url (this will be expanded to several urls in the future) is visited while the app is installed (to keep kids off porn or employees off facebook). Thus far I've been able to build the app that launches when a user visits test.com however the user will never naturally end up visiting customscheme://facebook.com So I need to figure out a way to launch my app (only) when visting specific sites in the browser with my app installed. – NoobNinja Mar 26 '13 at 18:47
  • @NoobNinja In that case, the user will have to set your app as the default for those URLs, which I highly doubt they will. You could look into replacing the entire browser, as that would give you finer control over visiting websites. – Raghav Sood Mar 26 '13 at 18:48
  • How can I (programatically) replace the entire browser? I've done a bit of research and haven't been able to find much information. http://stackoverflow.com/questions/5310489/create-custom-browser-for-android – NoobNinja Mar 26 '13 at 19:02
  • @NoobNinja You can't. Once more, the user must set your app as the default – Raghav Sood Mar 26 '13 at 21:55