Introduction to Broadcast receiver
suggest changeA Broadcast receiver is an Android component which allows you to register for system or application events.
A receiver can be registered via the AndroidManifest.xml file or dynamically via the Context.registerReceiver() method.
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//Your implementation goes here.
}
}
Here I have taken an example of ACTION_BOOT_COMPLETED, which the system sends once Android has completed the boot process. The app also needs RECEIVE_BOOT_COMPLETED. android:name=".MyReceiver" is the class in the application package. A name without the leading dot is looked up in the default package.
You can register a receiver in the manifest file like this:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
</action>
</intent-filter>
</receiver>
</application>
Once the device has booted, onReceive() is called and then you can do your work (for example, start a service or an alarm).
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents