Broadcast receiver
Definition
A broadcast receiver is a class which extendsBroadcastReceiver
and which is registered as a receiver in an Android Application via
the AndroidManifest.xml
file(or via
code).
Alternatively to the this static registration, you can also register a
BroadcastReceiver
dynamically
via the Context.registerReceiver()
method.
This class will be able to receive intents. Intents can be generated via the
Context.sendBroadcast()
method.
The class
BroadcastReceiver
defines
the onReceive()
method. Only during this
method your BroadcastReceiver
object
will be valid, afterwards the Android system can recycle the
BroadcastReceiver
. Therefore you cannot
perform any asynchronous operation in the onReceive()
method.
Sending Broadcast Intents
ThesendBroadcast()
method allows to
send Broadcast Intents
. You cannot
trigger system Broadcasts, the Android system will prevent this.
But you can define intent-filters for your own actions and trigger them via the
sendBroadcast()
method.
The following AndroidManifest.xml file show a
BroadcastReceiver
which is registered to a custom action.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="de.vogella.android.receiver.own" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <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> <receiver android:name="MyReceiver" > <intent-filter> <action android:name="de.vogella.android.mybroadcast" /> </intent-filter> </receiver> </application> </manifest>You can trigger this event via the
sendBroadcast()
method.
Intent intent = new Intent();
intent.setAction("de.vogella.android.mybroadcast");
sendBroadcast(intent);
Sticky Broadcast Intents
A normal broadcastIntent
is not
available anymore after is was send and processed by the system. If
you use the sendBroadcast(Intent)
method, the Intent
is sticky, meaning
the Intent
you are sending stays around
after the broadcast is complete.
You can can retrieve that data through the return value of
registerReceiver(BroadcastReceiver,
IntentFilter)
. This works also for a null BroadcastReceiver
.
In all other ways, this behaves the same as
sendBroadcast(Intent)
.
The Android system uses sticky broadcast for certain system information. For example the battery status is send as sticky
Intent
and can get received at any time. The following example demonstrates
that.
// Register for the battery changed event IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); / Intent is sticky so using null as receiver works fine // return value contains the status Intent batteryStatus = this.registerReceiver(null, filter); // Are we charging / charged? int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1); boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL; boolean isFull = status == BatteryManager.BATTERY_STATUS_FULL; // How are we charging? int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB; boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
with example
The
concept of Broadcast Receivers, one of the fundamental blocks of
Android, is very simple. These are applications that will respond to
any intent that is broadcast by other applications. Provided the
broadcast intent matches the intent specified against the receiver in
the AndroidManifest.xml
This
goes to automatically imply that many activities, events, services or
the like can broadcast intents expecting the appropriate action to be
automatically taken. So, to begin with, let us see the various
Broadcast events that are given by the platform itself. Here is a
standard list obtained from the android documentation:
·
ACTION_TIME_TICK
·
ACTION_TIME_CHANGED
·
ACTION_TIMEZONE_CHANGED
·
ACTION_BOOT_COMPLETED
·
ACTION_PACKAGE_ADDED
·
ACTION_PACKAGE_CHANGED
·
ACTION_PACKAGE_REMOVED
·
ACTION_PACKAGE_RESTARTED
·
ACTION_PACKAGE_DATA_CLEARED
·
ACTION_UID_REMOVED
·
ACTION_BATTERY_CHANGED
·
ACTION_POWER_CONNECTED
·
ACTION_POWER_DISCONNECTED
·
ACTION_SHUTDOWN
For
details on when each of these intents get broadcasted, please see the
android
documentation.
I have chosen the BROADCAST event ACTION_TIME_CHANGED as I can
simulate a time change in the emulator. How to simulate the time
change from adb shell is given at the end of this tutorial.
Any
activity that intends to respond to broadcasts has to extend the
android.content.BroadcastReceiver
class and
implement the single method onReceive().
In
my example, I just notify on the status bar that the time has changed
and the moment the user clicks on the status bar and sees the
details, clicks on the details, the notification is removed from the
status bar.
When
the user clicks on the detailed portion, I take the user to the
contacts application, just for anything better. Ideally this should
take to an activity relevant to the application. So, if you see the
onReceive() method,
it is nothing but a notification example. That is all.
private
NotificationManager
mNotificationManager;
private
int
SIMPLE_NOTFICATION_ID;
@Override
public
void
onReceive(Context
context, Intent intent) {
mNotificationManager
=
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notifyDetails =
new
Notification(R.drawable.android,"Time
Reset!",System.currentTimeMillis());
PendingIntent myIntent =
PendingIntent.getActivity(context, 0, new
Intent(Intent.ACTION_VIEW,
People.CONTENT_URI),
0);
notifyDetails.setLatestEventInfo(context,
"Time
has been Reset", "Click
on me to view Contacts", myIntent);
notifyDetails.flags
|=
Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(SIMPLE_NOTFICATION_ID,
notifyDetails);
Log.i(getClass().getSimpleName(),"Sucessfully
Changed Time");
}
Once
this class is ready, start the android emulator in eclipse. Then,
simulate a time changed from the command prompt as given below. You
will see the notification come up.
Simulating
a time change in the emulator:
To
start the adb shell type (in windows, assuming the path has been set
to the tools folder of android sdk installation):
C:\>
adb shell
#date
–- 2009-10-01 14:24:59
20070325.123456
#date
–s 20070325.123456
The
first step date –-
gives the time in
seconds since Jan 1st 1970. Take the result and give it as
a parameter to date –s,
the time is reset in adb and within a minute on the android emulator.
This broadcasts the event that time has been changed in the emulator
and kicks off the Broadcast Receiver program that has been executed.
Example
here Broadcst Receiver
to get a Toast when sms receieve
package
com.ann;
import
android.app.Notification;
import
android.app.NotificationManager;
import
android.app.PendingIntent;
import
android.content.BroadcastReceiver;
import
android.content.Context;
import
android.content.Intent;
import
android.provider.Contacts.People;
import
android.util.Log;
import
android.widget.Toast;
public
class
notification extends
BroadcastReceiver {
NotificationManager
mNotificationManager;
int
Notofication_id;
@Override
public
void
onReceive(Context context, Intent intent) {
Log.i("lkkjh",
"sms");
Toast.makeText(context,"sms
received"
,Toast.LENGTH_LONG).show();
}
}
AndroidManifest
<?xml
version="1.0"
encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ann"
android:versionCode="1"
android:versionName="1.0"
>
<uses-sdk
android:minSdkVersion="8"
/>
<uses-permission
android:name="android.permission.RECEIVE_SMS"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
>
<receiver
android:name="notification"
>
<intent-filter
>
<action
android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
<uses-library
/>
</application>
</manifest>
This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic.Android Training in chennai | Best Android Training in chennai|Android Training in chennai with placement | Android Training in velachery
ReplyDelete