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 




6 comments:

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

    ReplyDelete
  2. How do I know if a Wordpress theme supports a subscribe option? car insurance hacks

    ReplyDelete
  3. 강남 유흥에 관한 모든 정보들을 집대성하였습니다. 2025년 최신 강남 유흥에 대한 모든 정보를 편하고 손쉽게 원클릭만으로 알아보실 수 있도록 하였습니다. 최고의 밤, 프라이빗한 공간에서 강남 최고의 핫플레이스를 만나보세요! 강남 유흥에 관한 모든 정보들을 집대성하였습니다. 2025년 최신 강남 유흥에 대한 모든 정보를 편하고 손쉽게 원클릭만으로 알아보실 수 있도록 하였습니다. 최고의 밤, 프라이빗한 공간에서 강남 최고의 핫플레이스를 만나보세요! 강남 유흥

    ReplyDelete
  4. I was rattling pleased to find this site on bing, just what I was looking for : D also bookmarked . judi bola

    ReplyDelete