Automatically starting an Android application on boot

There can be multiple reasons for you to want to automatically start (autostart) an Android application or service after booting your device. Lets have a look at what a colleague of mine wrote.

To autostart an Android application there are a couple of steps you need to take. However before you do this you should consider this:

Background service awareness

When manually or even automatically starting an application, users will see something being started.
If you (auto)start a background service it won’t be visible to the user. Users might not be aware of the fact that a service was started. Keep this in mind when using a service.

Battery life

As we know by now people might not be aware that your automatically started background service is running. Keep the battery life of the phone in mind when performing tasks in the background.

To create a battery aware service use these things in moderation:

  • Getting a GPS or network location
  • Connecting to your network and sending/receiving data
  • Read/write files (IO)
  • Intensive tasks

Let’s autostart our service

First of all we need to make sure your application is allowed to receive and handle the broadcast that tells the application that your phone was booted.
To be able to receive the broadcast you should add this permission as a child of the <manifest> element to your Android Manifest:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

So, right now our application has the permission to receive a broadcasted message to tell us the phone was booted. In order to do anything with this broadcast we need to have a BroadcastReceiver. An example would look like this:

public class ExampleBroadcastReceiver extends BroadcastReceiver
  public void onReceive(Context context, Intent intent)
    if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction()))
      /* Only perform this code if the BroadcastReceiver received the ACTION_BOOT_COMPLETED action.*/

What we see here is a class that extends the BroadcastReceiver and overrides the onReceive method. We could add our code to start our background service inside the if statement. This way we make sure that only the ACTION_BOOT_COMPLETED action is being processed. This gives us the opportunity to implement code for different Intent actions if necessary.
Read the onReceive documentation for some do’s and dont’s.

An important thing to know is that the onReceive method will be called whenever the class is declared in your manifest and wishes to receive the specified Intent. Let’s let our application know that our BroadcastReceiver exists and wishes to receive the android.intent.action.BOOT_COMPLETED intent. Add this to your <application> element of your Android Manifest:

<receiver android:enabled=”true” android:name=”” android:permission=”android.permission.RECEIVE_BOOT_COMPLETED”>
    <action android:name=”android.intent.action.BOOT_COMPLETED” />

Check and double check

So to make sure you got everything covered check if you did this:

  1. Add the android.permission.RECEIVE_BOOT_COMPLETED permission as a child of your <manifest> element in your Android Manifest
  2. Create a class that extends BroadcastReceiver and add your code to the onReceive method
  3. Add this BroadcastReceiver class to your Android Manifest as a child of the <application> element so Android knows of it’s existence and it can trigger the onReceive method if the class matches the intent-filter

Enjoy autostarting your application

If all went well you can turn off your phone and turn it back on. After it has been booted your application should receive a broadcast from android that triggers your code. Automatically starting an Android application on boot, done!

Leave a Reply


captcha *

This site uses Akismet to reduce spam. Learn how your comment data is processed.