Running code on phone boot

SDK Version: 

Some applications, for example a mail client, needs to run in the background all the time. You may want to run code right after the device booted.

The device is booting

In the Android system BroadcastReaceivers are just for such mechanism, they can listen for defined intents with given parameters, and run code when they receive one, even when your application is not running at the moment. You can define own actions and fire the manually to control your receivers, but there are a lot of predefined broadcast in the system that are fired automatically on certain events.
One of the is the BOOT_COMPLETED action. The complete list is available here.

All you need to do is a class that extends Broadcastreceiver:

  1. public class HelloReceiver extends BroadcastReceiver {
  2.   @Override

Syndicate content