Custom URL Scheme In Android

Android

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

Custom URL Scheme

Custom URL Scheme is an awesome and interesting concept in Android development. Using this method, a user can open the Android application from a 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 following.

Step 1

Creating a 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 create a new project.

  2. Name the project as your wish and select your activity template.

    Android

  3. Click “Finish” button to create a 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.

  1. <intent-filter>  
  2.     <action android:name="android.intent.action.VIEW" />  
  3.   
  4.     <category android:name="android.intent.category.DEFAULT" />  
  5.     <category android:name="android.intent.category.BROWSABLE" />  
  6.   
  7.     <data  
  8.         android:host="your_host"  
  9.         android:scheme="your_scheme" />  
  10. </intent-filter>  

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't open particular Activity. The Intent Filter should have the following action and categories.

  1. <action android:name="android.intent.action.VIEW" />  
  2. <category android:name="android.intent.category.DEFAULT" />  
  3. <category android:name="android.intent.category.BROWSABLE" />  

The URL Scheme 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.

  1. Intent intent = getIntent();  
  2. if(Intent.ACTION_VIEW.equals(intent.getAction())){  
  3.     Uri uri = intent.getData();  
  4.     String post_id = uri.getQueryParameter("post_id");  
  5. }  

Sample URI Calling method

You can call your application’s activity from another, like below.

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

Download Code

You can download the full source code from the top of the article.