Auto-Start Foreground Service in Android

Perfect way to run service even Android device restarted

CodingwithSaud
The Startup
4 min readDec 18, 2020

--

Today I’m going to teach you about restartable foreground service even phone will turn off and turn on again or even rebooted the device. It is the perfect way to restart the foreground service and run it continuously. So first discuss the basic concept of foreground service

What is the foreground service?

Now let me tell you a little bit about foreground service. I didn’t take much time to explain it. Basically, foreground service is a background process that remains in a running state even app closed and removed from the recent list.

Let’s start developing an auto-start foreground service.

First, you need to declare two permissions in the manifest file.

a) <uses-permission android:name=”android.permission.FOREGROUND_SERVICE”/>

This permission is required for running the foreground permission which tells that your app going to run the foreground service. If you want to read in detail about this service then you can follow this link.

b) <uses-permission android:name=”android.permission.RECEIVE_BOOT_COMPLETED” />

This permission is required to trigger the broadcast receiver when the Android device is restarted or turn on. If you want to explore it more you can go to this link.

Here is the Manifest file.

Then go to the layout folder in your Android Studio project and make two buttons, one for starting the service and the second for stoping it.

Here is my layout code of main_activity.xml

Yeah, come to the main part of this article. Now I’m going to create a service class named “AutoStartService” and extend it with the Service class. Service is nothing but used to run the process in the background even App is killed. It is useful for running heavy and time taking processes. So it makes your Android App reliable.

So after extending the service class you should implement required override methods like onCreate, onDestory, onStartCommad, and onBind.

OnStartCommand will call when service started. So we need to make a notification in OnStartCommand method. It is very important to create a notification because without the notification we can’t keep running our service. The android operating system will kill the service after some time without displaying the notification.

Before creating the foreground service class we should make a class that extends with the application class which is given below.

In the above MyApp class is created. This class extends the Application class and after creating this class we should mention MyApp class in our manifest file Like this.

Manifest file

MyApp class will run automatically when the application is launched. In this class, there are two methods available one is the onCreate method. This is an overridden method. onCreate method will be called when the app is launched and the second method is createNotificationChannel. In this method, I’m creating a notification channel for our foreground service. It is a very important thing.

Are you excited to make a foreground service that will run forever? Let’s create a class for our foreground service which runs automatically even phone is turn off and turn on again.

You should mention this AutoStartService class in the manifest file which I show you at the start of this article.

Let's explore this class. This class extends service class. Service is nothing but needed to create our foreground service or normal service. If you want to learn about the Service class you can read it by going to this link.

After extending with Service class, I need to implement override methods like onCreate, onStartCommand, onDestroy, and onBind.

Let’s explain these methods.

  1. onCreate

This method will be called when service is created and it will be called before onStartCommand or onBind methods. If the service is already in running mode then the onCreate method will not be called.

2. onStartCommand

This method will be called when the service is started by startService() or stopService() method from activity or fragment etc.

3.onBind

This method will be invoked when the bindService() method will be called. This method is used to communicate with the service class. If you don’t want to communicate with the service class then you should return null in the onBind method. This method is necessary to implement in the service class.

4. onDestroy

This method will be invoked when service will available anymore or being destroyed. In this method, you can free the resources which will not require further.

Now come to the MainActivity.java file.

In the MainActivity.java file, I created two methods startService and stopService. Both methods are called onClick event of the buttons which we designed earlier in this article.

It is very simple to start service and stoping the service.

That’s it.

Thanks for reading my article till the end. 🙏

You will also like this article. Click on the below link.

--

--

CodingwithSaud
The Startup

I’m a good husband, father and Senior Developer.