Launch Android app 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.

 

[su_button url=”https://github.com/androidsrc/LaunchAppFromUrl” target=”blank” style=”stroked” background=”#51d461″ color=”#ffffff” size=”6″ center=”yes” radius=”0″ icon=”icon: arrow-circle-o-down”]Download Full Source Code[/su_button]

 

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.

<?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

<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.

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.

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.

Log.d("AndroidSRC", intent.toUri(Intent.URI_INTENT_SCHEME));<br>

At Browser side, it will look as below

<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>

You may also like...

3 Responses

  1. Manohar Perepa says:

    Is it possible if the application is not installed then can we redirect to playstore from the browser itself using the above code

  2. Lada says:

    Thank you very much! :]

  1. February 4, 2017

    […] I want to launch my custom android app from my web app on browser. I have followed the article here http://androidsrc.net/launch-android-application-from-browser-using-uri/. And it worked, I successfully trigger my app when web app user will click on link. The problem is […]

Leave a Reply

Your email address will not be published. Required fields are marked *