Basics of Android : Part II – Intent Receivers
In the last post we see about the Android activities and how to use them.
In this post we will see about the IntentReceivers.
Android Intent receivers are part of the event handling mechanism. The intent receiver can handle the broadcaste intents and thus can be used as event handler.
Creating Intent Receivers:
Android has defined a ‘BroadcastReceiver’ class. All the intent receivers have to inherit from this class. Following is the example of the intent receiver
package com.wissen.testApp.receiver;public class MyIntentReceiver extends BroadcastReceiver {
/**
* @see adroid.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent)
*/
@Override
public void onReceive(Context context, Intent intent) {
...
}
}
The intent receiver has to override the method onReceive as shown above. The onReceive method gets called when intent receiver’s intent is broadcasted, and thus onReceive() method is the entry point for the intent receiver.
The intent receiver entry has to be done in the AndroidManifest.xml file. The intent receiver can be defined in manifest xml as follows:
<receiver android:name=”.receiver.MyIntentReceiver” android:enabled=”true”> <intent-filter> <action android:name=”com.wissen.testApp.MY_INTENT_RECEIVER” /> </intent-filter> </receiver>
The intent receiver can also be registered dynamically with the help of Context.registerReceiver() method as follows:
..
MyIntentReceiver intentReceiver = new MyIntentReceiver();IntentFilter intentFilter = new IntentFilter("com.wissen.testApp.MY_INTENT_RECEIVER");
registerReceiver(intentReceiver, intentFilter);
..
Broadcasting Intent:
The intent can be broadcasted with the help of sendBroadcast() and sendOrderedBroadcast() method of the Context class. The sendBroadcast method will send the broadcast to all the registered Intent Receivers, on the other hand sendOrderedBroadcast() method calls the intent receivers one at a time. This gives the intent receivers ability to exchange result of previous intent receiver or to abort the broadcast. The order of execution is controls by the android:priority attribute defined in the <intent-filter> tag of AndroidManifext.xml file.
Receiver Lifecycle:
There is only one lifecycle method for the intent receiver as onReceive(). The method gets called when intent is broadcasted. The receiver object is only valid for the duration of the onReceive() method, after the method ends the object is considered to be non-active and can be garbage collected. Because of this the intent receiver onReceive() method should not handle any asynchronous operation.
To receiver an intent by the intent receiver the application does not need to be running. When the intent is broadcasted the system will start the application to call the intent receiver.
Permissions:
Sometimes for defining intent receivers for some intent, permissions need to be specified by the intent receiver. The intent receiver can specify the required permission in the AndroidManifest.xml file with the help of <user-permission> tag. While registering the intent receiver dynamically, the permission can be specified as the parameter of the registerReceiver method.
To enforce a permission while broadcasting an intent, a non-null permission argument need to be specified for the sendBroadcast() or sendOrderedBroadcast() method.
This is all on the intent receivers. In next post we will see android services and content providers.
Hi. Useful post. Thanks
Heii Thnxa sheff
Can U please tell me How Can I delete All the SMS of my Inbox,Even I want to delete them before Comi in the Inbox.
Please Do needful
Thanx
February 26th, 2010 at 09:13 | #2
Reply | Quote
Heii Thnxa sheff
Can U please tell me How Can I delete All the SMS of my Inbox,Even I want to delete them before Comi in the Inbox.
Please Do needful
Thanx
How do you send the Intent?
I know how to send e.g. a PHONE Intent,
but how to send com.wissen.testApp.MY_INTENT_RECEIVER
thanks
Hi james,
I created small application on “How to set intent”,just check out following application,defiantly help you.
http://www.androidcompetencycenter.com/wp-content/uploads/2010/06/TestIntent.zip
I tried TestIntent. No joy. I have this code sofar.
—-status bar app——-
Uri uri2 = Uri.parse(“55″);
Intent intent2 = new Intent(“com.wissen.testApp.MY_INTENT_RECEIVER”, uri2);
sendBroadcast(intent2);
–then in my widget I have this code which never hits MyIntentReceiver-