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

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


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 linkif (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.


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.


Here is my cut to the chase contribution.

Create an intent filter in the activity you want to load in the manifest like this:

<intent-filter>    <action android:name="com.bubblebeats.MY_CUSTOM_ACTION" />    <category android:name="android.intent.category.DEFAULT"/>    <category android:name="android.intent.category.BROWSABLE"/></intent-filter>

Your web page URL for this will look like this:

intent:#Intent;action=com.bubblebeats.MY_CUSTOM_ACTION;end

The most basic HTML code to launch your apk from a browser would look like this:

<body>    <a href="intent:#Intent;action=com.bubblebeats.MY_CUSTOM_ACTION;end">click to load apk</a></body>

To add variables to your intent

You can generate the URI from within your Android code like this:

Intent i = new Intent();i.setAction("com.bubblebeats.MY_CUSTOM_ACTION");i.putExtra("some_variable", "123456");Log.d("ezpz", i.toUri(Intent.URI_INTENT_SCHEME));

This will produce this:

04-13 09:47:30.742: DEBUG/ezpz(9098): intent:#Intent;action=com.bubblebeats.MY_CUSTOM_ACTION;S.some_variable=123456;end

You just want this part:

intent:#Intent;action=com.bubblebeats.MY_CUSTOM_ACTION;S.some_variable=123456;end

Take a good look at what occurred here and you can see how you can skip the code step and manually create these URIs yourself.

Especially this part:

S.some_variable=123456