[Android]Start Activity by Custom URL Scheme

If you want start activity, you write code like this.

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

And If you want launch url page, you will pass Uri with Intent.ACTION_VIEW.

String url ="http://gun0912.tistory.com";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);

If you use many browser in your phone, android will show select browser view for url load.
This select view is not only url also any other duplication action.

How you can see this select view?

Because that applications use same custom scheme (like ‘http://', ‘xxx://’)

Let’s see example.
This is notification view about user’s action.
(Look like facebook or instagram’s notification view)

If you click item of list, we will move to activity.
Post’s row id is 10 and Activity name is ‘Activity_Post_Detail’.
Then you can write code like this.
Make new intent instance and pass String extra.

Intent intent = new Intent(this,Activity_Post_Detail.class);
intent.putExtra(Activity_Post_Detail.STATE_POST_ID,"10");
startActivity(intent);

But If you use ‘Custom URL’, you can write code like this.
(Look like load url page)

String url =”selphone://post_detail?post_id=10";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);

This way don’t need Activity name. (need only url path)
Furthermore you can launch activity at your browser

Custom URL Scheme

1. Declare host / scheme at your activity (AndroidManifest.xml)

In this example, schme is selphone and host is post_detail

(Custom url will selphone://post_detail)

<activity
android:name=".post.Activity_Post_Detail"
android:theme="@style/Theme.Selphone.Transparent"

android:windowSoftInputMode="stateAlwaysHidden|adjustResize">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="kr.co.selphone.MainActivity" />

<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:host="post_detail"
android:scheme="selphone" />
</intent-filter>
</activity>

Now If you load ‘selphone://post_detail?…’ url, this activity will launch.

2. If you want to pass parameter to Activity, pass by query string

If you want pass post_id ( id: 10), you can add query string end of url.
“selphone://post_detail?post_id=10”

3. In your Activity, get your parameter at query string

Intent intent = getIntent();
if (Intent.ACTION_VIEW.equals(intent.getAction())) {
Uri uri = intent.getData();
String temp_post_id = uri.getQueryParameter(STATE_POST_ID);
}

Check intent action, and get query parameter from intent data.

Do it your self custom url activity for your application
If you want to know more detail infomation, check this page Android Intent Fileters