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