Android Start Service on Boot Automatically

Android Start Service on Boot Automatically

Android Start Service on Boot Automatically (Autostart Android Application)

Here we will learn about Android Start Service on Boot Automatically (Autostart Android Application). That means we will learn the simplest way to start an Android service or an application at device boot up. It is very simple. First we will register an Android event called ‘android.intent.action.BOOT_COMPLETED’. Then we will capture this event when boot up completed and start our service. Then we will start our app from the service.

Step 1:

First we need an Android  service to started at device boot up. Your service looks like below. If you are new to Android service then i recommend you to learn about simple Android service with example. I have written a very nice article on Android Services.

import android.app.Service;
import android.os.IBinder;
import android.content.Intent;

public class AndroidStartServiceOnBoot extends Service {

@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
super.onCreate();
// do something when the service is created
}

Step 2:

Now we need a class which extends from BroadcastReceiver class. In this class there is a method called onReceive() that will be called when the boot event is completed. From the onReceive() method we will start the Android service which we have just created in above step 1.

import android.content.Context;
import android.content.BroadcastReceiver;
import android.content.Intent;

// here is the OnRevieve methode which will be called when boot completed
public class BootCompleted extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
//we double check here for only boot complete event
if(intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED))
{
//here we start the service
Intent serviceIntent = new Intent(context, AndroidStartServiceOnBoot.class);
context.startService(serviceIntent);
}
}
}

Step 3:

Now We need do 3 things in Android Manifest file.

1. Add the permission in Manifest file to capture the boot complete event.

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

2. Register your BroadcastReciever class in Manifest file to receive the boot complete event.

<receiver
android:name=”.BootCompleted”
android:enabled=”true”
android:exported=”false”>
<intent-filter>
<action android:name=”android.intent.action.BOOT_COMPLETED” />
</intent-filter>
</receiver>

3. Register your Service class in Manifest File which we need to start when boot completed.

4. Your Manifest file looks like below.

Note: The above Manifest file is just an example for Android Start Service on Boot Automatically.

 Step 4: Start Android Application at Device boot up.

Now if you want to start (launch) an Android activity at device boot, then start the activity from the service instead of start it from the receiver.

Tips:

  • The onReceive() function in the receiver class expire with in 8 seconds, if you will do any time consuming task or waiting more than 8 seconds in onReceive() function, then your application will crash with ANR. So better to start a service and from the service you can do a time consuming task.

I hope this small tutorial on ‘Android Start Service on Boot Automatically‘ will help you at it’s best.

Leave a Reply

Your email address will not be published. Required fields are marked *