Introduction
In this article, we will learn how to open the Android Application from your browser or hyperlink if it is installed on your Android phone.
Custom URL Scheme
The 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 follows.
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 a New Project with Android Studio
- Open Android Studio and create a new project.
- Name the project as you wish and select your activity template.
- Click the “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 the Custom URL Scheme. Here, you must specify the host and path of the URL scheme.
- <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="your_host"
- android:scheme="your_scheme" />
- </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.
- <action android:name="android.intent.action.VIEW" />
- <category android:name="android.intent.category.DEFAULT" />
- <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.
- Intent intent = getIntent();
- if(Intent.ACTION_VIEW.equals(intent.getAction())){
- 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:
- Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
- startActivity(intent);
Download Code
You can download the full source code from the top of the article.