Thursday 21 May 2015

Android example of Deep Linking

Hi friends'

Here i am posting an example of android DEEP LINKING.

In this simple application two activities are created.
MainActivity and
DeepLinking Activity

In manifest file you need to write which activity you want to open.
In this example DeepLinking Activity is set to open.

Manifest file code:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="example.deeplinking" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/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>
        </activity>
        <activity
            android:name=".DeepLinking"
            android:label="@string/title_activity_deep_linking" >
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <data android:scheme="http"
                      android:host="www.example.in"
                      android:pathPrefix="/deep" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
            </intent-filter>
        </activity>
    </application>

</manifest>

MainActivity class code:

package example.deeplinking;

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


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

activity_main layout file:

<?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:gravity="center">

    <TextView
        android:text="Main Activity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"/>

</LinearLayout>

DeepLinking Activity code:

package example.deeplinking;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;


public class DeepLinking extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_deep_linking);
        Intent intent = getIntent();
        String action = intent.getAction();
        Uri data = intent.getData();

        TextView deepLinkTV = (TextView) findViewById(R.id.deepLinkTV);
        deepLinkTV.setText("Action: "+action+"\n URI: "+data.toString());
    }
}


activity_deep_linking layout file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:gravity="center">

    <TextView
        android:id="@+id/deepLinkTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="20sp"/>

</LinearLayout>


In order to test type the following in command line:
//your uri and package name
adb shell am start -a android.intent.action.VIEW -d "http://www.example.in/deep" example.deeplinking

The sample output: