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.
4. activity_main.xml, The layout file for MainActivity.java
5. activity_one.xml, The layout file for ActivityOne.java
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
Search within Codexpedia
Search the entire web