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

The question:

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" rel="nofollow noreferrer noopener">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!

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

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

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

Method 3

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" rel="nofollow noreferrer noopener">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

Method 4

Because hackbod never gave us code-examples, I just want to share mine, after I got this to work.

First of all, you need to define a custom action in your manifest file:

<activity
    android:name=".activity.MainActivity"
    android:label="@string/app_name_full">
    <intent-filter>
        <action android:name="com.yourpackage.action.OPEN_VIEW"></action>
        <category android:name="android.intent.category.DEFAULT"></category>
        <category android:name="android.intent.category.BROWSABLE"></category>
    </intent-filter>
</activity>

Then, for the content on your website, you need to generate the URI from an intent.
Put following code in your Activity (This code could be removed, once the link is generated):

Intent i = new Intent();
        i.setAction("com.yourpackage.action.OPEN_VIEW");
        i.setPackage("com.yourpackage");
        i.putExtra("myextra","anystring");
        Log.d(getClass().getSimpleName(), i.toUri(Intent.URI_INTENT_SCHEME));

To receive the Extras, put following in your activity, that is able to recieve the custom action (as defined in manifest):

final Intent intent = getIntent();
final String action = intent.getAction();

        if ("com.yourpackage.action.OPEN_VIEW".equals(action)) {
           Log.i(getClass().getSimpleName(), "EXTRA: "+intent.getExtras().getString("myextra"));
        }

On your website (this is the previously generated link):

<a href="intent:#Intent;action=com.yourpackage.action.OPEN_VIEW;package=com.yourpackage;S.myextra=anystring;end" rel="nofollow noreferrer noopener">Open App with extra</a>

Hope that helps someone for better understanding. Please correct me, if I got something wrong.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment