By Manohar Reddy

2019-01-28 10:01:26 8 Comments

There are few questions similar to this on StackOverFlow but none of the solutions are working for me


The problem is with only few devices like OnePlus and MI ,The service is getting killed as soon as the user swipes away app from recent app.

I read that these OEM'S use some aggressive strategies to kill services . I just want to know is there any way to keep service running or start it as soon as it gets killed .

What I want

I need to run a service which will give location continuously (24/7) in background (This app is only for specific people so no worries about battery life) .

Things I tried till now :

1) Tried to run foreground service .

     } else {

also in service onCreate method started with notification

public void onCreate() {
    Log.i("Service", "onCreate");
    startForeground(NOTIFICATION_ID, getnotification());

2) Tried to return START_STICKY in onStartCommand

 public int onStartCommand(Intent intent, int flags, int startId) {


    return START_STICKY;

3) Tried to re initiate service in onDestroy and onTaskRemoved but they are not getting called .

4) Tried to bind a service

5) I can schedule alarm manager and start service frequently but play store will give warning that our app is using alarm manager too frequenly and its a bad practice . And there is now way using workmanager to schedule for less than 15 min and its still not guaranteed to start after 15 min .

So is there any way to keep running a service other than above options ?


@user3329544 2019-04-10 15:07:53

The right way to listen to location on the background of to use a fused location API from play services.

Look at the samples here.

These API's are power efficient, and I would recommend it too.

@Manohar Reddy 2019-01-31 09:43:57

Here are few things which helped little .

In AndroidManifest.xml add line android:enabled="true"


Inside service Add alarm to wake up again after 2 seconds .

public void onTaskRemoved(Intent rootIntent) {

   private void initAlarm() {
    AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this, StartServiceReceiver.class);
    PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

            SystemClock.elapsedRealtime() +
                    2000, alarmIntent);


Create a receiver StartServiceReceiver and in it just start service again .

For Mi devices we need to allow a permission inside setting to allows service to start in background

@ankuranurag2 2019-01-31 10:00:18

If you go through THE LINK, you will find:

Unfortunately, some devices implement killing the app from the recents menu as a force stop. Stock Android does not do this. When an app is force stopped, it cannot execute jobs, receive alarms or broadcasts, etc. So unfortunately, it's infeasible for us to address it - the problem lies in the OS and there is no workaround.

It is a known issue. To save battery, many manufacturers force close the app, thus cancelling all the period tasks, alarms and broadcast recievers etc. Major manufacturers being OnePlus(you have option to toogle),Redmi,Vivo,Oppo,Huwaei.

Each of these devices have AutoStartManagers/AutoLaunch/StartManager type of optimization managers. Which prevent the background activities to start again. You will have to manually ask the user to whitelist your application, so that app can auto start its background processess. Follow THIS and THIS link, for more info.

The methods to add to whitelist for different manufactures are give in this stackoverflow answer. Even after adding to whitelist, your app might not work because of DOZE Mode, for that you will have to ignore battery otimizations

Also in case you might be wondering, apps like Gmail/Hangout/WhatsApp/Slack/LinkedIn etc are already whitelisted by these AutoStart Managers. Hence, there is no effect on their background processes. You always receive timely updates & notifications.

@pelya 2019-01-31 16:11:46 has most up-to-date instructions for each individual device

Related Questions

Sponsored Content

28 Answered Questions

28 Answered Questions

[SOLVED] How to check if a service is running on Android?

11 Answered Questions

[SOLVED] Service vs IntentService

46 Answered Questions

[SOLVED] Is there a unique Android device ID?

94 Answered Questions

[SOLVED] Close/hide the Android Soft Keyboard

25 Answered Questions

[SOLVED] Is there a way to run Python on Android?

77 Answered Questions

28 Answered Questions

[SOLVED] What is 'Context' on Android?

11 Answered Questions

[SOLVED] Proper use cases for Android UserManager.isUserAGoat()?

1 Answered Questions

[SOLVED] android service startForeGround functionality

Sponsored Content