MobileAndroidGetting Your Android App Listed in the App Chooser for Various Activities

Getting Your Android App Listed in the App Chooser for Various Activities

Introduction

As mobile operating systems evolve, the number of built-in apps is increasing. However, as app developers, we want users to use our application instead of other available apps. In this article, we will learn how we can make our app as one of the available choices when the user is prompted to select an application in the App chooser.

Basics

The Android operating system offers the use of intent filters to signal that our application is a candidate for certain actions/protocols when the user interacts with the device. Intent filters are declarations that are made in the app manifest file, which is a signal to the operating system about what actions the application can respond to. Intent filters respond to implicit intents, which specifies an action that can invoke any app capable to handle the intent.

The format for specifying an intent filter is as follows:

<intent-filter android:icon="drawable resource"
                       label="string resource"
                       priority="integer" >

   <action android_name="INTENT_ACTION"/>
   <category android_name="INTENT_CATEGORY"/>
   <data android_mimeType="DATA TYPE"/>
</intent-filter>

The intent filter declaration is done at an activity level in the app manifest file.

The syntax requires an <activity> element to be present in an intent filter and can also contain <category> and <data> elements.

The icon represents the parent activity, or service that the application is declaring the capability for. The value should be a drawable resource.

The priority attribute should be used only if you desire to impose a specific order in which broadcasts are received, or if you want Android to choose a specific activity over another. The values can range between –1000 and 1000 (the default is 0).

Note that each intent filter needs to list at least one action, but it can list more than one. On the other hand, an intent filter can list zero or more categories.

The following intent filters are available to register for the various activities:

  • ACTION_MAIN
  • ACTION_VIEW
  • ACTION_ATTACH_DATA
  • ACTION_EDIT
  • ACTION_PICK
  • ACTION_CHOOSER
  • ACTION_GET_CONTENT
  • ACTION_DIAL
  • ACTION_CALL
  • ACTION_SEND
  • ACTION_SENDTO
  • ACTION_ANSWER
  • ACTION_INSERT
  • ACTION_DELETE
  • ACTION_RUN
  • ACTION_SYNC
  • ACTION_PICK_ACTIVITY
  • ACTION_SEARCH
    ACTION_WEB_SEARCH
    ACTION_FACTORY_TEST

The most common of the above are ACTION_VIEW (when the user wants to view some data) and ACTION_SEND (when the user want to send data) and we will use them in our demo.

Hands On

Let us create a simple application that uses Intent filters.

Fire up Android Studio and click to Start a new Android Project.

List01

Figure 1: Starting a new Android Project

Provide IntentFilterDemo as the Application Name and click Next.

List02

Figure 2: Providing an application name

On the next screen, leave the default values and click Next.

List03

Figure 3: Leaving the default values in place

On the “Add an activity to Mobile” page, choose “Blank Activity”. This creates an application with a single activity.

List04

Figure 4: Choosing a blank activity

We then are prompted to customize the activity. We will leave the default values unchanged.

List05

Figure 5: Again, leaving the default values in place

Click Finish to create the project files.

Next, we add a second Activity “SecondActivity” to the project. This activity will declare the intent filters so that it enables the application to show up an as an option when a specific action is triggered.

List06

Figure 6: Adding a second Activity

Lastly, we add a third Activity that will handle events to share data. This activity is called “ThirdActivity”.

List07

Figure 7: Adding a third Activity

Now, we need to declare the intent filters for the two activities we created.

Open up AndroidManifest.xml and make the changes as shown in the next piece of code. You can see that, for SecondActivity, we are registering the intent filter to handle a HTTP request (like when someone clicks a link). For ThirdActivity, we will make this activity listen for a user activity to share text-based content.

Changes from the default are highlighted.

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

   <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=".SecondActivity"
         ndroid:label="@string/title_activity_second">
         <intent-filter>
            <action android_name=
               "android.intent.action.VIEW"/>

            <category android_name=
               "android.intent.category.DEFAULT"/>

            <data android_scheme="http"/>
         </intent-filter>
      </activity>
      <activity
         android:name=".ThirdActivity"
         android:label="@string/title_activity_third">
         <intent-filter>
            <action android_name=
               "android.intent.action.SEND"/>
            <category android_name=
               "android.intent.category.DEFAULT"/>
            <data android_mimeType="text/plain"/>
         </intent-filter>
      </activity>
   </application>
</manifest>

As we can see, we have declared SecondActivity as a processor for the VIEW action, and the ThirdActivity as a processor for the SEND action.

Now, we need to make UX changes to trigger the events. We will do that by using buttons.

Open the design view for MainActivity and add a couple of buttons one button to initiate a HTTP Web request for “devcomstg.wpengine.com” and the second button to “share text content”.

Next, we will implement the click event handlers for the buttons.

In MainActivity.java, add a method as shown:

// Click Event handler for "Visit devcomstg.wpengine.com" button
public void onbuttonClick(View v) {
   Intent i = newIntent(android.content.Intent.ACTION_VIEW,
      Uri.parse("http://18.220.208.18"));
   startActivity(i);
}

This activity is meant to simulate user action when they click a link. In our case, we have hardcoded this to devcomstg.wpengine.com.

Next, we implement the click event handler for the “Click to Share” button:

// Click Event handler for "Click to Share" button
public void onbutton2Click(View v) {
   // specifying ACTION_SEND as the intent action,
   //so that the registered handlers can listen
   //and process the intent
   Intent sendIntent = newIntent();
   sendIntent.setAction(Intent.ACTION_SEND);
   sendIntent.setType("text/plain");
   startActivity(sendIntent);
}

Our event handlers are ready. We now need to wire up the event handlers.

Open up the text view for mainactivity.xml and make the following changes to register the click event handlers.

<RelativeLayout xmlns_android=
      "http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin"
   tools:context=".MainActivity">

   <TextView android_text="@string/hello_world"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/textView"/>

   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Visit devcomstg.wpengine.com"
      android:id="@+id/button"
      android:layout_below="@+id/textView"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="47dp"
      android:onClick="onbuttonClick"/>

   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Click to Share"
      android:id="@+id/button2"
      android:layout_centerVertical="true"
      android:layout_alignLeft="@+id/button"
      android:layout_alignStart="@+id/button"
      android:onClick="onbutton2Click"/>

</RelativeLayout>

Our demo application is now complete. When we launch the application, you will see the following screen.

List08

Figure 8: The application, launched

If you click the “Visit devcomstg.wpengine.com” button, you will notice the App choose dialog.

Because I have already done this one, I am being offered a variation of the app chooser dialog which highlights “SecondActivity” as the recommended activity to intercept my intent.

List09

Figure 9: A variation of the app chooser dialog

Similarly, if I click the “Click to share” button, I am offered the “ThirdActivity” as the recommended activity to handle my intent.

List10

Figure 10: You’re offered a third Activity

Summary

In this article, we learned how applications can declare intent filters and become one of the apps listed in the App chooser dialog when the user conducts a specific action. You can download a copy of the demo from the link at the end of this article.

About the Author

Vipul Patel is a technology geek based in Seattle. He can be reached at vipul.patel@hotmail.com. You can visit his LinkedIn profile at https://www.linkedin.com/pub/vipul-patel/6/675/508.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories