Home > Android > Start Service at boot

Start Service at boot

June 29th, 2009

Hello,

Now we are going to learn how to start a service at boot time, i.e. to start our service when device starts up.

First we have to create a BroadcastReceiver which will be started when boot completes as

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

public class MyStartupIntentReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
}
}

onRecieve will be first called when the BroadcastReceiver MyStartupIntentReceiver starts.

Next make an entry of this receiver in AndroidManifest.xml as

<receiver android:name="MyStartupIntentReceiver">
<intent-filter>
<action
android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>

Here in intent filter we have declared the action as android.intent.action.BOOT_COMPLETED , so that this receiver and intent with action android.intent.action.BOOT_COMPLETED

Now this receiver will be intimated when boot completes

Next if we want to start a service then the procedure is as follows

Create a Service as


import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service {

	@Override
	public IBinder onBind(Intent intent) {

		return null;

	}

	@Override
	public void onCreate() {
		super.onCreate();

		Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();

	}

	@Override
	public void onDestroy() {
		super.onDestroy();

		Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();

	}

	@Override
	public void onStart(Intent intent, int startId) {

		super.onStart(intent, startId);

		Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();

	}
}

Make an entry of this service in AndroidManifest.xml as

<service android:name="MyService">
<intent-filter>
<action
android:name="com.wissen.startatboot.MyService" />
</intent-filter>
</service>

Now start this service in the BroadcastReceiver MyStartupIntentReceiver’s onReceive method as

	public void onReceive(Context context, Intent intent) {
		Intent serviceIntent = new Intent();
		serviceIntent.setAction("com.wissen.startatboot.MyService");
		context.startService(serviceIntent);

	}

Next you can write the code that you want to execute when device boots up in the BroadcastReceiver or in the Service

Android , , , ,

  1. Brandon
    June 29th, 2009 at 10:30 | #1

    My service creates a TimerTask, however, android stops my timertask, right after it runs once. It only stays alive, if an Activity is launched on bootup, which starts my timertask & service…

    Whats going on?

  2. Donny
    December 16th, 2009 at 17:23 | #2

    I don’t think this work in the emulator. The ActivityManager / PackageManager keep on uninstalling my service process because it is not a system process.

  3. zeeshan
    December 17th, 2009 at 00:14 | #3

    @Donny
    Hi Donny ,
    For testing in emulator, don’t start emulator by right clicking and running the application for the second time. Start the emulator directly by command line.

  4. Franklin
    March 8th, 2010 at 06:06 | #4

    Obviously, you also need to add the permission android.permission.RECEIVE_BOOT_COMPLETED.

  5. June 9th, 2010 at 09:54 | #5

    this is very helpful, thank you very much!

  6. jfcoer
    June 20th, 2010 at 23:55 | #6

    It is very useful tutorial information.

  7. sanjeev
    June 26th, 2010 at 16:55 | #7

    THanks man..

  8. Ankit
    July 21st, 2010 at 02:05 | #8

    i created the same application as writen here but when i run my application emulator is not showing it in application list….whot shud i do…

  9. Ankit
    July 21st, 2010 at 02:38 | #9

    application is showing in application manager….but it is not doing any thing when i restart the emulator from command line

  10. July 27th, 2010 at 06:30 | #10

    Thanks a lot for a wonderful post.

  1. No trackbacks yet.