How to register some URL namespace (myapp://app.start/) for accessing your program by calling a URL in browser in Android OS?

So I want to create an Android app so it would be registered somewhere in android OS (or just would start on system start) and when phone user clicks on special button on a web page inside a web browser a la:

 <a href="myapp://mysettings">Foo</a> 

my app would pop up and run using the params sent in that URL.

So how do I do such thing?

I need a tutorial with code!


Solution 1:

First, to be able to start your app from link with custom scheme 'myapp' in browser / mail, set intent filter as follows.

<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>

and to parse queries in your link myapp://someaction/?var=str&varr=string
(the code is over simplified and has no error checking.)

Intent intent = getIntent();
// check if this intent is started via custom scheme link
if (Intent.ACTION_VIEW.equals(intent.getAction())) {
  Uri uri = intent.getData();
  // may be some test here with your custom uri
  String var = uri.getQueryParameter("var"); // "str" is set
  String varr = uri.getQueryParameter("varr"); // "string" is set
}

[edit] if you use custom scheme to launch your app, one of the problem is that: The WebView in another apps may not understand your custom scheme. This could lead to show 404 page for those browser for the link with custom scheme.

Solution 2:

You need to follow the standard rules for URIs via the W3C and such, which basically means: do not do this.

Android defines a Uri syntax for describing a generic Intent. There are methods on Intent for converting to and from this representation, such as: http://developer.android.com/reference/android/content/Intent.html#toUri(int)

So the way to do this is to use the normal facilities to describe an in your manifest for the kinds of intents you are going to handle with a particular component, especially defining an action name in your own namespace (com.mycompany.myapp.action.DO_SOMETHING or whatever). You can then make an Intent that matches your component, and use Intent.toUri() to get the URI representation of this. This can be placed in your link, and will then when pressed look for something that handles and and thus find your app. Note to be launched from the browser like this, the component's must handle the BROWSABLE category. (You don't need to have this in the Intent you put in the link, the browser will automatically add this in for you.)

Finally, you may want to set the package of the intent to your app with this: http://developer.android.com/reference/android/content/Intent.html#setPackage(java.lang.String)

This is a newer feature in the platform, which allows you to direct link intents to only your app so that other applications can not intercept and handle them.

In summary: read the regular documentation on intents and intent filters (such as the NotePad tutorial, though you won't be using content: URIs here, probably just custom actions) and get your app working that way. Then you can make a browser link to launch your app in the same way, provided your intent-filter handles the BROWSABLE category.