Launch android application from Browser using Uri of Launch Intent
Ever wondered, How do we launch application from Browser? For applications already installed in your device, if you visit eCommerce like amazon, ebay or entertainment website they will show you link to navigate to application or continue with browser.
If we launch app at some later stage, they will resume same display category or video resumed from last running time. We will check it with sample application.
After creating and installing sample application in your device, Click following link. Click here to launch sample app It will launch sample application.
Create a new application with package name “com.androidsrc.openappfromurl”. Activity layout will have a textview which will show text retrieved from values passed from Uri.
Edit your application manifest file, add a new IntentFilter to your activity with action as “com.androidsrc.launchfrombrowser” and define category to be default and browseable. Final AndroidManifest.xml will be as below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.androidsrc.openappfromurl" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <action android:name="com.androidsrc.launchfrombrowser" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> </intent-filter> </activity> </application> </manifest> |
Create /res/layout/activity_main.xml as below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.androidsrc.openappfromurl.MainActivity" > <TextView android:id="@+id/launch_info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="@string/hello_world" android:textSize="55dp" android:textStyle="bold" /> </RelativeLayout> |
Finally, We will retrieve intent that started activity in onCreate(). If intent action matches to that of what we defined in browser link, we will retrieve extra values passed from it. We can use these values to redirect/show view as required. MainActivity.java will be as below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package com.androidsrc.openappfromurl; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends Activity { private static final String LAUNCH_FROM_URL = "com.androidsrc.launchfrombrowser"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView launchInfo = (TextView)findViewById(R.id.launch_info); Intent intent = getIntent(); if(intent != null && intent.getAction().equals(LAUNCH_FROM_URL)){ Bundle bundle = intent.getExtras(); if(bundle != null){ String msgFromBrowserUrl = bundle.getString("msg_from_browser"); launchInfo.setText(msgFromBrowserUrl); } }else{ launchInfo.setText("Normal application launch"); } } } |
Let’s move over to browser. Now we will see how to create intent Uri to define in html tag that launched our application.
Let’s understand how to create Uri. Define Intent in your usual way. Add action and category for intent, add extras if you want.
1 2 3 4 5 6 |
Intent intent = new Intent("com.androidsrc.launchfrombrowser"); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.addCategory(Intent.CATEGORY_BROWSABLE); Bundle bundle = new Bundle(); bundle.putString("msg_from_browser", "Launched from Browser"); intent.putExtras(bundle); |
To convert above Intent to Uri, use toUri() api with flag passed as Intent.URI_INTENT_SCHEME. For above defined Intent, you will get Uri as intent:#Intent;action=com.androidsrc.launchfrombrowser;category=android.intent.category.DEFAULT;category=android.intent.category.BROWSABLE;S.msg_from_browser=Launched%20from%20Browser;end
If you try logging above intent, you will get same output.
1 |
Log.d("AndroidSRC", intent.toUri(Intent.URI_INTENT_SCHEME));<br> |
At Browser side, it will look as below
1 |
<a href="intent:#Intent;action=com.androidsrc.launchfrombrowser;category=android.intent.category.DEFAULT;category=android.intent.category.BROWSABLE;S.msg_from_browser=Launched%20from%20Browser;end">Click here to launch sample app</a> |