I am trying to run a background service in an android application. I have tried the standard way of doing it and a lot of other tweaks. The problem is that when the application is closed from recent apps, the service gets killed. This only happens in Chinese Android devices like Oppo, Vivo, Xiomi, etc. It works fine on all other brands.
I have tried the following solutions.
Over-riding the
OnStartCommand()
of activity to returnSTART_STICKY
. This still not re-starts activity after the application is closed as expected.@Override public int onStartCommand(Intent intent, int flags, int startId) { return START_STICKY; }
Added the application to exceptions of battery saving, so that it doesn't close it to save battery in
DOZE
mode.Gave the application permission to
Auto-Start
from the security settings of phone.Started the service as a foreground service, including a persistent notification.
@Override public int onStartCommand(Intent intent, int flags, int startId) { String input = intent.getStringExtra("inputExtra"); createNotificationChannel(); Intent notificationIntent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) .setContentTitle("Foreground Service") .setContentText(input) .setSmallIcon(R.drawable.ic_launcher_foreground) .setContentIntent(pendingIntent) .build(); startForeground(1, notification); //do heavy work on a background thread //stopSelf(); return START_NOT_STICKY; }
Implemented Alarm Manager approach of starting service after it gets killed in
onTaskRemoved()
andonDestroy()
methods of service.<service android:name=".MyService" android:enabled="true" android:exported="true" android:stopWithTask="false" />
@Override public void onTaskRemoved(Intent rootIntent){ Log.d("Service:","I am being closed!"); Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass()); restartServiceIntent.setPackage(getPackageName()); PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT); AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE); alarmService.set( AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 1000, restartServicePendingIntent); super.onTaskRemoved(rootIntent); }
I have also tried using a broadcast manager to start my service receiving a broadcast from activity when it is closed.
<receiver android:name=".SensorRestarterBroadcastReceiver" android:enabled="true" android:exported="true" android:label="RestartServiceWhenStopped" />
public class SensorRestarterBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.i(SensorRestarterBroadcastReceiver.class.getSimpleName(), "Service Stops!"); context.startService(new Intent(context, MyService.class));; } }
However, the service is not started again in these chinese devices specifically. While background services of Whatsapp, Facebook and some famous apps return after a few minutes of closing the app. Please suggest the correct way to achieve this.
I have tried solutions provided at Background Service is not restarting after killed in oppo, vivo, mi android version 7.1.2 as also described above. It doesn't solve my problem.