Friday, February 29, 2008

IntentReceiver only application

IntentReceiver only application

Google Docs: This document is published at http://docs.google.com/Doc?id=ddwc44gs_27dwrnrwrk
WARNING: Blogspot engine change the link, so you have to cut and paste.

If you want to react in some way to one or various Intents and you don't need an Activity you can have an IntentReceiver only application. You class must extend IntentReceiver and you have to override onReceiveIntent method.

This has been asked many time in android groups, but there was no clear answer and ApiDemos doesn't show an example of this either. So I hope this helps and saves you some time.

Create AndroidSampleIntentReceiver project

Create AndroidSampleIntentReceiver project as usual.

You can use Dummy as the Activity and Application name. We will remove them shortly.

Delete Dummy.java

Go to the project and delete Dummy.java right clicking on it and selecting Delete from menu.

Create SampleIntentReceiver class

Right clicking on the com.codtech.android.training.sampleintentreceiver or whatever you've selected for your package, create a new Java class


and edit its content to have

/**
* Copyright © 2008 Diego Torres Milano <dtmilano at gmail dot com>
*/

package com.codtech.android.training.intentreceiver;

import android.content.Context;
import android.content.Intent;
import android.content.IntentReceiver;
import android.util.Log;
import android.widget.Toast;

/**
* @author diego
*
*/

public class SampleIntentReceiver extends IntentReceiver {
private static final String TAG = "SampleIntentReceiver";

/*
* (non-Javadoc)
*
* @see android.content.IntentReceiver#onReceiveIntent(android.content.Context,
* android.content.Intent)
*/

@Override
public void onReceiveIntent(Context context, Intent intent) {
String msg = "SampleIntentReceiver:nonReceiveIntent(context="
+ context.getPackageName()
+ ", intent=[" + intent.getAction() + ", " + intent.getType()
+ ", " + intent.getData() + "])";
Log.d(TAG, msg);
Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
}

}

Modify AndroidManifest.xml

Refactoring the code doesn't change the AndroidManifest.xml, perhaps a deficiency of the Eclipse ADT plugin, so we need to manually edit it. Our intention is to have an application consisting only in an IntentReceiver, with no Activity and this is precisely what we need to remove from this file.

Use the XML view in the editor.

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

<application android:icon="@drawable/icon">
<receiver android:name=".SampleIntentReceiver">
<intent-filter>
<action android:name="com.codtech.android.training.intent.SAMPLE_ACTION"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.GET_CONTENT"/>
<data android:mimeType="image/*"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
</application>
</manifest>

We have just added some sample intent-filters, you can add the ones you are interested in.

Install the application

Install you application APK using

 $ adb install AndroidSampleIntentReceiver/bin/AndroidSampleIntentReceiver.apk

Once the application is installed, there's no need to run it. Android will be responsible to run it and invoke onReceiveIntent.

Broadcast an Intent

From your application code, that is another application you want it to broadcast some Intents that will be “trapped” by our SampleIntentReceiver. For example

   Intent intent = new Intent("com.codtech.android.training.intent.SAMPLE_ACTION");
// do something
broadcastIntent(intent, null);

and if everything went well


7 comments:

Jeeva Viswanathan said...

Hi Diego,
i tryed ur code but i m unable to run it
with out a activity its not running can u explin how to run it

Diego Torres Milano said...

You need an Intent that matches the filter you've defined in your IntentReceiver's AndroidManifest.xml.
Intents can be generated by Intent Playground (http://dtmilano.blogspot.com/2008/03/android-playing-with-intents-this.html)

Jeeva Viswanathan said...

i mena how to create the AndroidSampleIntentReceiver.apk

Diego Torres Milano said...

As explained in the original post:
1) Create project
2) Delete Dummy.java
3) Create SampleIntentReceiver class
4) Copy code
5) Edit AndroidManifest.xml
6) Build
7) Install

Jeeva Viswanathan said...

I got this Error

Application Error: Home
An error has occured in home unable to find explicit activity call{com.codtech.android...smapleIntentRecever};have you declared this activity in your AndroidManifest.xml?.

Damien said...

Hi Diego

I found your example very useful and have re-jigged your onReceiveIntent logging mechanism in a demo of my own - I haven't published it yet but when I do it will go on sleepydroid.blogspot.com.

Thanks
Damien

Diego Torres Milano said...

Hi,
I'm glad to hear that you've found it useful.
Please let me know when you publish your example.