[TUT] Add Your App to the Android ‘do something’ Menu (using Intent Filters)

This request is to be able to add your application to the context menus of the android system.

When the android system or another application wants to do a specific task, it will broadcast an intent saying “can anyone do this for me“. Here I’ll show you how you can grab this request so your the one that helps!

The specific tutorial request was to be added to the menu when someone using the Android messaging application hits the ‘attach’ button. I was looking through the Android source code for this and sadly we can’t actually get into the ‘attach’ menu. However you can get selected when the user presses ‘Picture’ or ‘Video’, or any of the other selections.

There’s not much coding to this Google have kept Intents nice and clean. What we need to do is update the intent-filters in our manifest to declare that we are there to handle these broadcasts. An intent-filter is just a way of saying, I am capable of handling this type of data.

Here is the manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.blundell.mediaattach"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".ui.phone.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.GET_CONTENT" />

                <category android:name="android.intent.category.DEFAULT" />

                <data android:mimeType="*/*" />
            </intent-filter>
        </activity>
    </application>

</manifest>

This application has one activity called MainActivity, we have removed the Main action and this means you won’t be able to see the app installed in the apps menu.

We’ve added a new intent filter for ‘Get Content’ meaning if an application requests ‘content’ we are there and will be nominated as one of the applications you can use!

The data tag holds the mime type of any data we can handle, using */* means we can handle all data. If you used image/* you could handle image requests only.
As we are using */* when your activity is started you will need to ask for the type of the data that was sent to you and act accordingly (as you could be sent anything!).

There are many intents that you can capture and this is an example of just one, if you wanted to be used in a different way take a look through the list here. Anything starting with ACTION_ can be used as an intent filter, also this page gives a good insight into how the Manifest constructs intent filters.

Here is the activity that will be started, it doesn’t do anything but thats up to you to implement ;-)

MainActivity.java

package com.blundell.mediaattach.ui.phone;

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

import com.blundell.mediaattach.R;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // Get intent, action and MIME type
        Intent intent = getIntent();
        String action = intent.getAction();
        String type = intent.getType();
        
        // You would then use these three variable to figure out who has sent you an Intent and what they want you to do!
        // See here for further instruction: http://developer.android.com/training/sharing/receive.html
        // http://developer.android.com/guide/topics/manifest/action-element.html
        // http://developer.android.com/reference/android/content/Intent.html#ACTION_GET_CONTENT
        
    }
}

Thats it! You app will now be shown in the menu when a user chooses attach > pictures.

Because there isn’t much coding I haven’t attached the source, but if you want it just comment and ask. Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

6 thoughts on “[TUT] Add Your App to the Android ‘do something’ Menu (using Intent Filters)

  1. Hi,

    I have an app where i have some images in a gridview and i want that it the images in the girdview can be attached to whats app attachments and any other chat app’s attachements. I am able to add my app in the share menu of what’s app but I don’t know how to attach the image from my app and send it back to what’s app. Can you help me.

    The action i am recieving when my app is launched from what’s app’s share menu is
    action PICK. How do i attach my image and send it back?

  2. With having so much content and articles do
    you ever run into any issues of plagorism or copyright infringement?
    My site has a lot of unique content I’ve either created myself or outsourced but it looks like a lot of it is popping it up all over the web without my agreement. Do you know any techniques to help prevent content from being stolen? I’d genuinely appreciate it.

    1. Probably, I don’t actually check, if anyone copies I just take it as a compliment, it’s all free. Also I’m sure my website would always come first in search results anyway