activity

Android Start Service on Boot Example

In the mobile device world, many times, we may need to make an application that has to do some tasks in the background, without the users to have to open their application. Some easy examples are the background Location tracking, or an alarm application, or the applications that want to receive push events from a server.

This can be done with the use of a service that will run in the background and will start on device boot, or on application update in a device. In order to achieve this, we are going to register a BroadcastReceiver to listen to RECEIVE_BOOT_COMPLETED events and then start a service to do whatever we may want our service to do.

So, in this example, we are going to show how to make an application that has an Android service that starts service at device boot.

For our example will use the following tools in a Windows 64-bit or an OS X platform:

  • JDK 1.7
  • Android Studio 1.3.2
  • Android SDK 5.0

Let’s take a closer look:

1. Create a New Android Studio Project

Tip
You may skip project creation and jump directly to the beginning of the example below.

Open Android Studio and choose “Start a new Android Studio Project” in the welcome screen.

“Welcome to Android Studio” screen. Choose “Start a new Android Studio Project”.
“Welcome to Android Studio” screen. Choose “Start a new Android Studio Project”.

Specify the name of the application, the project and the package.

“Configure your new project” screen. Add your application name and the projects package name.
“Configure your new project” screen. Add your application name and the projects package name.

In the next window, select the form factors your app will run on.

“Target Android Devices” screen.
“Target Android Devices” screen.

In the next window you should choose “Add no activity”. In this example, we are going to create our Activity.

“Add an activity to Mobile”. Choose: “Add no activity”.
“Add an activity to Mobile”. Choose: “Add no activity”.

Now, our project has just been created!

2. Create the layout and the source code of a simple AndroidStartServiceOnBoot Activity

Add a new Java class Activity inside src/com.javacodegeeks.androidserviceonbootexample/ so that we are going to have the src/com.javacodegeeks.androidserviceonbootexample/AndroidCustomFontExample.java file and paste the code below.

MainActivity.java

package com.javacodegeeks.androidserviceonbootexample;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="This Activity does nothing at all. But an auto-start service has just started!"
        android:textColor="#272727"
        android:textSize="20dp" />
</LinearLayout>

Actually, this activity will not do anything concerning the service that starts on boot. All the logic regarding the service is explained in the next lines.

3. Creating the source code of the BroadcastReceiverOnBootComplete Service

Add a new Java class inside src/com.javacodegeeks.androidserviceonbootexample/ so that we are going to have the src/com.javacodegeeks.androidserviceonbootexample/BroadcastReceiverOnBootComplete.java file and paste the code below.

BroadcastReceiverOnBootComplete.java

package com.javacodegeeks.androidserviceonbootexample;

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

public class BroadcastReceiverOnBootComplete extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
            Intent serviceIntent = new Intent(context, AndroidServiceStartOnBoot.class);
            context.startService(serviceIntent);
        }
    }
}

We have just created a BroadcastReceiver that will receive the ACTION_BOOT_COMPLETED intent. This means that when we boot up our device this class will “catch” the event and start the AndroidServiceStartOnBoot service.

4. Creating the source code of the AndroidServiceStartOnBoot Service

Add a new Java class inside src/com.javacodegeeks.androidserviceonbootexample/ so that we are going to have the src/com.javacodegeeks.androidserviceonbootexample/AndroidServiceStartOnBoot.java file and paste the code below.

AndroidServiceStartOnBoot.java

package com.javacodegeeks.androidserviceonbootexample;

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

public class AndroidServiceStartOnBoot extends Service {

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

    @Override
    public void onCreate() {
        super.onCreate();
       // here you can add whatever you want this service to do
    }

}

5. Editing the Android Manifest xml

The AndroidManifest.xml of our project is :

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.javacodegeeks.androidserviceonbootexample">

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

    <receiver
        android:name="com.javacodegeeks.androidserviceonbootexample.BroadcastReceiverOnBootComplete"
        android:enabled="true"
        android:exported="false">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.PACKAGE_REPLACED" />
            <data android:scheme="package" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.PACKAGE_ADDED" />
            <data android:scheme="package" />
        </intent-filter>
    </receiver>

    <service android:name="com.javacodegeeks.androidserviceonbootexample.AndroidServiceStartOnBoot"></service>

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

        <activity
            android:name="com.javacodegeeks.androidserviceonbootexample.MainActivity"
            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
>

In this AndroidManifest.xml we register the application to listen to android.intent.action.BOOT_COMPLETED. Additionally, we register the android.intent.action.PACKAGE_REPLACED and the android.intent.action.PACKAGE_ADDED events. These events can catch the installation and update of our application, so that our service will start not only on device boot, but on application update.

We also have to register our service in the AndroidManifest.xml. This happens at line 24.

6. Build, compile and run

When we build, compile and run our project, the main Android ServiceStart OnBoot application should look like this:

This is this how our application should look like.
This is this how our application should look like.

This application does not do anythining in its activity. All the work is done in the service that starts to run on the boot of our device, or on update of our application.

This is the service that runs in our device.
This is the service that runs in our device.

This is the service that runs in our device.
This is the service that runs in our device.

7. Download the Android Studio Project

This was an example of Android AndroidServiceStartOnBoot.

Download
You can download the full source code of this example here: AndroidStartServiceOnBootExample

Chryssa Aliferi

Chryssa is a Computer Science graduate from Athens University of Economic and Business. During her studies, Chryssa carried out a great variety of projects ranging from networking to software engineering. She is very keen on front end development especially on mobile technologies and web applications. She has worked as a junior Software Engineer in the telecommunications area and currently works as an Android Developer.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Vikas Patidar
Vikas Patidar
5 years ago

You should declare service and receiver tags inside application tag in AndroidManifest.

Back to top button