Updated 11 February 2016
A Broadcast Reciever is an android component which allows us to register the system or application events. A Broadcast Reciever is simply called as Reciever. When an event happens the all registered receivers are notified.
For example, ACTION_BOOT_COMPLETED system event which is fired once the Android system has completed the boot process.
We can register a Reciever via two ways: static and dynamic.
Registering via AndroidManifest.xml is the static way and for dynamic registration, we can use Context.registerReceiver() method.
For implementing receivers, extends the BroadcastReceiver class.
Android system calls the onReceive() method If the event for which the broadcast receiver has registered happens.
Several system events are defined as final static fields in the Intent class. Other Android system classes also define events, e.g., the TelephonyManager defines events for the change of the phone state.
On system reboot, data can be automatically synchronized when a service started. For this, we can register a receiver for the android.intent.action.BOOT_COMPLETED system event. This requires the android.permission.RECEIVE_BOOT_COMPLETED permission in AndroidManifest.xml.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="*****" android:versionCode="2" android:versionName="1.0" > <uses-sdk android:minSdkVersion="13" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <application android:icon="@drawable/my_icon" 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="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> <service android:name=".MyService" /> </application> </manifest> |
1 2 3 4 5 6 7 8 9 10 11 |
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Intent intent = new Intent(context, MyService.class); context.startService(intent); } } |
3. Service
1 2 3 4 5 6 7 8 9 10 11 12 |
public class MyService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { //TODO do something return Service.START_NOT_STICKY; } @Override public IBinder onBind(Intent intent) { //TODO for communication with other components and return an instancce of IBinder return null; } } |
If our application is installed on sd-card then register it for the android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE event.
“Android API level 11 the user needs to have started the application at least once before an application can receive android.intent.action.BOOT_COMPLETED events.”
A pending intent is a token that you give to another application (e.g., notification manager, alarm manager or other 3rd party applications), which allows this other application to use the permissions of your application to execute a predefined piece of code.
To perform a broadcast via a pending intent, get a PendingIntent via the getBroadcast() method of the PendingIntent class. To perform an activity via a pending intent, you receive the activity via PendingIntent.getActivity().
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.