Saturday 30 August 2014

Open Android application from Web Browser

Most of us want to promote our applications. The easiest way is to put a link in the html page, whenever a user click on that link your application opens in Google Play or if it is installed then open the application itself.




Download Source Code Download 
Here for testing we will do it on localhost.
1. Requiement
  1. Download XAMPP from here.
  2. Install it.
  3. Then start Apache Server.
  4. Now your system becomes a server. Go to C:\xampp\htdocs (Where your XAMPP is installed) create www folder and then create OpenFromWEB folder inside www folder .This is how it will look.
  5. In index.html we will write our html code.
2. Index.html
  1. The pattern of the url that will open application is intent://host/#Intent;scheme=schemeName;package=packageName;end
  2. For sending information from URL to application intent://host/#Intent;scheme=schemeName;package=packageName;S.content=WebContent;end

String => 'S'
Boolean =>'B'
Byte => 'b'
Character => 'c'

Double => 'd'
Float => 'f'
Integer => 'i'
Long => 'l'
Short => 's'

<html>
<title>Open Android Application</title>
<head> 
</head>
<body>
<a href="intent://com.tutorialsface.openapplication/#Intent;scheme=launch;package=com.tutorialsface.openapplication;S.content=WebContent;end">Open Application</a><br>
<a href="intent://com.tutorialsface.apkextractorlite/#Intent;scheme=launch;package=com.tutorialsface.apkextractorlite;end">Open PlayStore Application</a>
</body>
</html>

To access this page you have to type in webBrowser ip/www/OpenFromWEB.
You can get the ip  by using ipconfig command in cmd.


Now you can access this web page anywhere in the world using the above URL.

2. Android Code

1. The main thing require is the entry in manifest. Add this in the Activity you want to open.
<intent-filter>
    <data
        android:host="com.tutorialsface.openapplication"
        android:scheme="launch" />

    <action android:name="android.intent.action.VIEW" />

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


2. To receive data sent from URL use this in onCreate method of your activity.
Bundle extras = getIntent().getExtras();
if (extras != null) {
      String content = extras.getString("content");

} else {}

3. For demo I created a sample project OpenApplication. Here is the manifest file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tutorialsface.openapplication"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity android:name="com.tutorialsface.openapplication.MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <data
                    android:host="com.tutorialsface.openapplication"
                    android:scheme="launch" />

                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.BROWSABLE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

4. MainActivity.java
package com.tutorialsface.openapplication;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity{

      TextView textView1;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            textView1 = (TextView) findViewById(R.id.textView1);
            Bundle extras = getIntent().getExtras();
            if (extras != null) {
                  String content = extras.getString("content");
                  textView1.setText("Content: " + content);
            } else {}
      }
}

5. main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:textSize="20dp" />

</LinearLayout>

Download Source Code Download 




3 comments:

  1. Hello, its only opening application on google store, even if application is installed. What am i doing wrong ? Thank you.

    ReplyDelete