The recommended way to start your App from Browser on Android

When someone looks for starting their app from the browser on Android most probably will find some solution that implies using a personal scheme declared in an intent filter with(see here and here). Also if he reads further he may see that the people from Google say that this is not recommended – “We strongly discourage people from using their own schemes, unless they are defining a new world-wide internet scheme.” What is the right way? If you are looking over the Internet you’ll hardly find something relevant, but here is the answer.

Basically instead of using own scheme we should use the “intent:” scheme with the appropriate intent string that uniquely represents our app and an unique “action”.

Step 1. get the appropriate string identifying this app, make sure it sets our own action and package, and any other extra params we’d like:

Intent i = new Intent();
i.setAction("com.our.test.action.OPEN_FROM_SIGNUP");
i.setPackage("com.our.test");
i.putExtra(LoginActivity.FROM_SIGN_UP, "usernamestring");
Log.v(getClass().getSimpleName(), "intent to uri " + i.toUri(Intent.URI_INTENT_SCHEME));

run and read the log to get the URI. After this step the code is not needed anymore and may be removed.

Step 2. you may use the URI you’ve just collected in html in something like this:

<a href="intent:#Intent;action=com.our.ttest.action.OPEN_FROM_SIGNUP;package=com.our.test;S.FROM_SIGN_UP=usernamestring;end">Open App with extra</a>

Step 3. add to manifest for the right activity the following filter:

<intent-filter>
<action android:name="com.our.test.action.OPEN_FROM_SIGNUP"></action>
<category android:name="android.intent.category.DEFAULT"></category>
<category android:name="android.intent.category.BROWSABLE"></category>
</intent-filter>

pay attention to the action that is custom and identical with the one set in our intent. Also that BROWSABLE is needed as category.

Step 4. in the app you can get and use the extras like this:

Log.v(getClass().getSimpleName(), "intent has the action " + getIntent().getAction());
if (getIntent().getExtras().containsKey(LoginActivity.FROM_SIGN_UP)){
    Log.v(getClass().getSimpleName(), "extras " + getIntent().getExtras().getString(LoginActivity.FROM_SIGN_UP));
}

Leave a Reply

You must be logged in to post a comment.