Home > Android > Start Service at boot

Start Service at boot

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

  • Brandon

    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?

  • Donny

    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.

  • zeeshan

    @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.

  • Franklin

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

  • http://leeminarchive.cafe24.com leemin

    this is very helpful, thank you very much!

  • jfcoer

    It is very useful tutorial information.

  • sanjeev

    THanks man..

  • Ankit

    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…

  • Ankit

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

  • http://www.androidpeople.com Dravid

    Thanks a lot for a wonderful post.

  • Gabor

    Thanks! Very usefull.

  • matejP

    Works great! tnx!

  • sunil

    Thanks very useful.

  • hemanta

    Thanks Its very usefull

  • hemanta

    thanks its working fine

  • http://www.aspinvision.com Drew

    Thanks for the code example!

  • lattimore

    Simple & Userful!

  • Maitv89

    Who can send source this application that work okie for me.
    mail: maitv89@gmail.com

  • Maitv89

    i can run same application…..Tks a lot.

  • Panpaliamahen

    Is there anyway that i can start an activity using either BroadcastReceiver or service?
    I have tried following piece of code and it crashes application.
     

  • http://twitter.com/MichaelNwanna Michael

    have you found the solution to the stopping of services started on boot_completed?

  • Hi

    What is the error you are getting?  Can you post some code?  I implemented a similar service that starts on boot.  It displays a notification in the notification bar persistently and when touched opens the corresponding application.

  • Sharl

    Great! it works on my app.thanks a lot!

  • Dev Rejupal

    Thanks working

  • http://twitter.com/SweetSniff Hassan A. Al-Jeshi

    Works like charm. Thank you for this great tutorial.

  • http://twitter.com/SweetSniff Hassan A. Al-Jeshi

    also wanted to point out that if you are on 2.1+ you have to change:
    public void onStart(Intent intent, int startId)
    to
    public int onStartCommand(Intent intent, int flags, int startId)
    in the service class

  • Findingpoint

    Can anybody explain how to stop (cancel) a service at boot ?

  • Findingpoint

    Can anybody explain how to stop (cancel) a service at boot ? 
    google_map for instance ?
    Are there any OS tweaks to do this?

  • http://babble.krslynx.com/?p=65 Android Useful Info | Nothing Interesting
  • Shashidhar Yamsani

    Thanks, Nice and simple. Got it working with one shot.

  • ApplicationBistro

    You can stop the service from starting at boot time by using a variable and an if statement in the onReceive method.  Add a preferences screen to your app and store a preference that enables/disables starting the service at boot time.  Then inside the onReceive method, get the value of the preference and test it using an if statement that will start the service if enabled and skip the service start code otherwise.

  • Dougler

    Example:

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

  • http://androidforums.com/application-requests/451150-app-volunteer-firefighters.html#post3562903 App for volunteer firefighters – Android Forums

    [...] Why start an alarm? Rather I think you need to register and intent filter for boot time like this: Start Service at boot | Android Competency Center [...]

  • Syed Ahmed

    its not working i did everything as mentioned , when i run it there is message that there are error in your project fix it, but it is not showing any errors

  • Sackofils

    Hi, I tried your example in my device (Archos 7oi android 2.2) but it doesn’t work porperly. It shows me a warning message as : “Sudden closure of the application … Test_Boot_Time (of the process com.android.test) Thank you to retry.”

    I don’t understand what isn’t fine there. Does anybody can help me please ?

  • Anonymous

    HI;
    I am developing an android application where the user enters his login and password. ThenI have to connect to an external database in order to check if the data entered by the userare correct. I can not recover the data entered by the user to check. someone could help me?