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:
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()); }
returning
START_STICKY
inonStartCommand
@Override public int onStartCommand(Intent intent, int flags, int startId) { initLocationClient(); initLocationSyncThread(); return START_STICKY; }
re-initiating service in
onDestroy
andonTaskRemoved
but they are not getting called.binding a service
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?