Android deep linking example

Deep linking in Android is to provide a way for directly going to a certain screen in an app when the app is launched through a URI (example://activityone) or URL (http://www.example.com/activityone). We are going to create a sample deep linking app with the following few steps.

1. MainActivity.java, this is the activity which will be launched when the app is launched, it has a button to send a notification message. When the notification message is clicked, it will launch ActivityOne.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d("MainActivity", "onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnOne = (Button) findViewById(R.id.btn_one);

        btnOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String msg = "Activity One";
                Intent intent = new Intent(getApplicationContext(), ActivityOne.class);
                sendNotificationMessage(intent, msg);
            }
        });
    }



    public void sendNotificationMessage(Intent intent, String subTitle) {
        // NotificationCompatBuilder is a very convenient way to build backward-compatible notifications.  Just throw in some data.
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(getApplicationContext())
                        .setColor(getResources().getColor(android.R.color.white))
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle("Deep Linking")
                        .setContentText(subTitle);

        // The stack builder object will contain an artificial back stack for the started Activity.
        // This ensures that navigating backward from the Activity leads out of your application to the Home screen.
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
        stackBuilder.addNextIntent(intent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(123, mBuilder.build()); // ID 123 allows you to update the notification later on.
    }
}

2. ActivityOne.java, this is the class we will use to demonstrate the deep linking. The deep linking configuration will be set in the AndroidManifest file for the activity tag of this class.

public class ActivityOne extends AppCompatActivity {

    TextView tvAction;
    TextView tvData;
    TextView tvQueryParam;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one);
        tvAction = (TextView) findViewById(R.id.tv_action);
        tvData = (TextView) findViewById(R.id.tv_data);
        tvQueryParam = (TextView) findViewById(R.id.tv_query_param);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //Read data from incoming intents
        Intent intent = getIntent();
        String action = intent.getAction();
        Uri data = intent.getData();

        tvAction.setText(action);
        if (data != null) {
            tvData.setText(data.toString());
            tvQueryParam.setText(data.getQueryParameter("movie"));
        }
    }
}

3. The deep linking configuration for the ActivityOne class. This should be in the application tag in the AndroidManifest file.

<activity android:name="com.example.deeplinking.ActivityOne">
    <intent-filter android:label="Activity One">
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <!-- Accepts URIs that begin with "http://www.example.com/activityone” -->
        <!-- note that the leading "/" is required for pathPrefix-->
        <data android:scheme="http" android:host="www.example.com" android:pathPrefix="/activityone" />
    </intent-filter>

    <intent-filter android:label="Activity One">
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <!-- Accepts URIs that begin with "example://activityone” -->
        <data android:scheme="example" android:host="activityone" />
    </intent-filter>
</activity>

4. activity_main.xml, The layout file for MainActivity.java

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/btn_one"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="18sp"
        android:text="Go to Activity One" />
</RelativeLayout>

5. activity_one.xml, The layout file for ActivityOne.java

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/tv_activity_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textSize="20sp"
        android:text="Activity One" />

    <TextView
        android:id="@+id/tv_action"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:gravity="center"
        android:layout_centerInParent="true"
        android:textSize="20sp"
        android:text=""/>

    <TextView
        android:id="@+id/tv_data"
        android:layout_below="@+id/tv_action"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:gravity="center"
        android:layout_centerInParent="true"
        android:textSize="20sp"
        android:text=""/>

    <TextView
        android:id="@+id/tv_query_param"
        android:layout_below="@+id/tv_data"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_centerInParent="true"
        android:textSize="20sp"
        android:text=""/>
</RelativeLayout>

6. Launch the AcitvityOne from shell command line window. The uri used here is “example://activityone?movie=SpiderMan” which is configured in the activity tag for the ActivityOne class in the AndroidManifest file.

adb shell am start -W -a android.intent.action.VIEW -d "example://activityone?movie=SpiderMan" com.example.deeplinking

7. The command line arguments for launching an activity through uri.

adb shell am start
 -W -a android.intent.action.VIEW
 -d <URI> <PACKAGE>

Search within Codexpedia

Custom Search

Search the entire web

Custom Search