Beruflich Dokumente
Kultur Dokumente
Home
About us
Contact us
Privacy Policy
Tutorials
Gadgets
Internet
Technologies
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
@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
<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>
<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.
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
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
C ONNECT
autostart android app on boot, how to start an android app on device bootup, launch android application on
device startup
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.
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
Search
Techblogon
Follow
+1
+ 305
Like 1,859 people like this. Be the first of your friends.
Recommend on Google
Follow @techblogon
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
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