Custom URL Scheme in Android

In this article, we will learn how to open Android Application from your browser or hyperlink, if it is installed in android phones.

Custom URL Scheme:

Custom URL Scheme is an awesome and interesting concept in android development. Using this method, user can open the android application from hyperlink. The implementation of this method is very simple. The same concept is available for iOS also.

Coding Part:

The implementation is split into 3 steps as in following.
Step 1: Creating New Project with Android Studio.
Step 2: Setting up AndroidManifest for the project
Step 3: Implementing the functionality to open the application.
Without any more introduction, we will jump into the coding part.

Step 1: Creating New Project with Android Studio:

1. Open Android Studio and Select Create new project.

2. Name the project as your wish and select your activity template.
3. Click “finish” button to create new project in Android Studio.
Step 2: Setting up AndroidManifest for the project:
This step is very important for the implementation of Custom URL Scheme. Here, you must specify the host and path of the URL Scheme
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

android:scheme="your_scheme" />

You should specify your host and scheme name with Intent Filter in your AndroidManifest.xml file against an Activity. Without specifying the name of the activity, you can open particular Activity. The Intent Filter should have the following action and categories.
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
The URL Scheme is looks like your_scheme://your_host For Example androidmad://post You can pass the query parameters through this URL to the application.
Ex: androidmad://post?post_id=1

Step 3: Implementing the functionality to open the application

In this step, we will see how to receive the data from the custom URL scheme. The URL is received using Intents.
Intent intent = getIntent();
Uri uri = intent.getData();
String post_id = uri.getQueryParameter("post_id");

Sample URI Calling method

You can call your Application’s activity from another like below
String  url =”selphone://post_detail?post_id=10";
intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));

Download Code:

You can download sample code from the following GitHub link.

Share this

Related Posts

Next Post »