3

There are few questions similar to this on Stack Overflow 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.

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).

I've tried:

  1. running foreground service.

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            startForegroundService(intent)
        } else {
                startService(intent)
        }
    

    Also in service onCreate method started with notification

    @Override
    public void onCreate() {
        Log.i("Service", "onCreate");
        startForeground(NOTIFICATION_ID, getnotification());
    }
    
  2. returning START_STICKY in onStartCommand

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    
        initLocationClient();
        initLocationSyncThread();
    
        return START_STICKY;
    }
    
  3. re-initiating service in onDestroy and onTaskRemoved but they are not getting called.

  4. binding a service

  5. scheduling alarm manager and start service frequently but play store will give warning that our app is using alarm manager too frequently 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?

double-beep
  • 5,031
  • 17
  • 33
  • 41
Manohar
  • 22,116
  • 9
  • 108
  • 144
  • related https://stackoverflow.com/questions/51289236/continually-running-background-service – AskNilesh Jan 28 '19 at 10:02
  • I started with notification but its still getting killed . – Manohar Jan 28 '19 at 10:03
  • https://stackoverflow.com/a/43719950/7666442 – AskNilesh Jan 28 '19 at 10:08
  • @NileshRathod tried that too but onDestroy is not getting called . – Manohar Jan 28 '19 at 10:10
  • Hello try to return START_NOT_STICKY instead of START_STICKY in your server class onStartCommand method . start not sticky will force to start service again after kill application – Navin Jan 28 '19 at 10:13
  • @Navin its opposite https://stackoverflow.com/a/33969065/6478047 – Manohar Jan 28 '19 at 10:16
  • 1
    related [https://stackoverflow.com/questions/53131441/a-foreground-service-gets-killed-and-notification-gets-removed-when-app-is-swipe](https://stackoverflow.com/questions/53131441/a-foreground-service-gets-killed-and-notification-gets-removed-when-app-is-swipe) and [https://stackoverflow.com/questions/53084293/android-kills-background-services-in-xiaomi-huawei-etc](https://stackoverflow.com/questions/53084293/android-kills-background-services-in-xiaomi-huawei-etc). – greeble31 Jan 28 '19 at 15:52
  • @ManoharReddy - Brother did you find the solution to keep Service alive 24/7. If yes, please help me in. – Irfan Akram Nov 15 '19 at 17:11
  • @IrfanAkram open https://dontkillmyapp.com/ click on the vendor which is causing you the most problem . At the bottom there will be "Solution for devs" . Follow the instructions and hope for the best – Manohar Nov 16 '19 at 11:35
  • I gone through it but there are different solutions for different vendors, also there is no solution for most of the vendors. I want a general solution regarding all the vendors. If you know some other way, then please. It would be great help. – Irfan Akram Nov 16 '19 at 11:51
  • @IrfanAkram I am afraid there is no common solution , try to exclude your app from battery optimization , it seems to help few people – Manohar Nov 17 '19 at 03:08

3 Answers3

5

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 receivers 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 Stack Overflow 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.

double-beep
  • 5,031
  • 17
  • 33
  • 41
ankuranurag2
  • 2,300
  • 15
  • 30
  • 5
    https://dontkillmyapp.com/ has most up-to-date instructions for each individual device – pelya Jan 31 '19 at 16:11
1

Here are few things which helped little .

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

<service
      android:name=".service.TrackingService"
      android:exported="false"
      android:enabled="true"
    />

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

@Override
public void onTaskRemoved(Intent rootIntent) {
    initAlarm();
    super.onTaskRemoved(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);

    alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            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

Manohar
  • 22,116
  • 9
  • 108
  • 144
0

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. https://github.com/googlesamples/android-play-location?files=1

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