You are on page 1of 7

15/07/2015

Android Start Service on Boot Automatically | Techblogon

Home
About us
Contact us
Privacy Policy

Tutorials
Gadgets
Internet
Technologies

Android Start Service on Boot Automatically


April 22, 2013 Android Tutorial, Tutorials

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. Click here to read.
1
2
3
4
5
6

import
import
import

public

android.app.Service;
android.os.IBinder;
android.content.Intent;
class AndroidStartServiceOnBoot extends Service {

http://techblogon.com/android-start-service-on-boot/

1/7

15/07/2015

7
8
9
10
11
12
13
14
15
16

Android Start Service on Boot Automatically | Techblogon

@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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

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.
1 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

2. Register your BroadcastReciever class in Manifest file to receive the boot complete event.
1
2
3
4
5
6
7
8

<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.
http://techblogon.com/android-start-service-on-boot/

2/7

15/07/2015

Android Start Service on Boot Automatically | Techblogon

1 <service android:name=".AndroidStartServiceOnBoot" ></service>

4. Your Manifest file looks like below.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.techblogon.loginexample"
android:versionCode="1"
android:versionName="1.0" >

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

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

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

<service android:name=".AndroidStartServiceOnBoot" ></service>

<activity
android:name="com.techblogon.loginexample.TestActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

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.
http://techblogon.com/android-start-service-on-boot/

3/7

15/07/2015

Android Start Service on Boot Automatically | Techblogon

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 its best.
Previous Page
Android Tutorial Home Page
Next Page

Post By SmrutiRanjan (57 Posts)

C ONNECT

Working @ Samsung as a Project Lead for Android


Smartphones. I have been blogging since 2008.
Previously I was writing articles for other bloggers, but
finally I have started my own blog-"Techblogon".I am also
an active contributor for the blog-"Gadgets n Gizmos
World". Job is my necessity, but blogging is my passion.
Website: Techblogon

autostart android app on boot, how to start an android app on device bootup, launch android application on
device startup

5 Responses to Android Start Service on Boot Automatically


1.

Hemant katariya says:


May 9, 2013 at 5:52 am
Please provide me the link for full code,
Bcz its force close
Reply

2.

Muhammad says:
June 24, 2013 at 1:06 pm
Nice tutorial, work fine
Reply

3.

pradeep says:
July 16, 2013 at 11:24 am
Really helpfull.Cool tutorial..
Thank you very much..:)
Reply

http://techblogon.com/android-start-service-on-boot/

4/7

15/07/2015

4.

Android Start Service on Boot Automatically | Techblogon

Imran says:
November 11, 2013 at 3:21 pm
sir i am doing exactly same but background service run multiple times
i want to run only once after boot complete.
Samsung glaxy S3(jelly bean 4.1.2)
Reply
SmrutiRanjan says:
November 15, 2013 at 4:39 am
Use a static boolian variable, once start your service, then make it true, and alwayd check this
variable before start your service.
Reply

Leave a Reply
Your email address will not be published. Required fields are marked *
Name *
Email *
Website
six = 24

Comment
You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym
title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del
datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" dataurl=""> <span class="" title="" data-url="">

Post Comment

http://techblogon.com/android-start-service-on-boot/

5/7

15/07/2015

Android Start Service on Boot Automatically | Techblogon

Search

Techblogon
Follow

+1

+ 305
Like 1,859 people like this. Be the first of your friends.

Recommend on Google

Follow @techblogon

216 follow ers

Enter your Email Address...


Subscribe

RSS Feed

Google Plus

Archives
March 2015
July 2014
April 2014
October 2013
August 2013
July 2013
http://techblogon.com/android-start-service-on-boot/

6/7

15/07/2015

Android Start Service on Boot Automatically | Techblogon

June 2013
May 2013
April 2013
March 2013
February 2013
January 2013
December 2012

Categories
Android
Android Tutorial
Design Patterns
Displays
E-Commerce
Gadgets
Google
Internet
Mobile Phones
Softwares
Technologies
Tutorials
Windows

Pages
About us
Contact us
Privacy Policy

Meta
Log in
Entries RSS
Comments RSS
WordPress.org

2015 Techblogon

http://techblogon.com/android-start-service-on-boot/

7/7

You might also like