Chinese Android Devices Killing Background Service
Asked Answered
A

2

2

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.

  1. Over-riding the OnStartCommand() of activity to return START_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;
        }
    
  2. Added the application to exceptions of battery saving, so that it doesn't close it to save battery in DOZE mode.

  3. Gave the application permission to Auto-Start from the security settings of phone.

  4. 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;
    }
    
  5. Implemented Alarm Manager approach of starting service after it gets killed in onTaskRemoved() and onDestroy() 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);
        }
    
  6. 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.

Amortizement answered 4/4, 2020 at 11:58 Comment(2)
Does this answer your question? Background Service is not restarting after killed in oppo, vivo, mi android version 7.1.2Rachal
No. Already tried that. Still the same problem.Amortizement
S
2

This will happen mostly for optimizing battery and improving phone performance,

As you have tried my solution but still you are getting this issue in some devices, so here is an alternate solution for killing app on swipe from recent app list,

  1. Ask User to Swipe down the application which will lock application as white list and wont be killed

OR

  1. Add below line for every activity in manifest, which will hide your app from recent app list. hence user cant swipe app from recent apps list.

    android:excludeFromRecents="true"

Stubble answered 10/4, 2020 at 8:39 Comment(3)
This is wonderful Amin. Excluding from recent worked like charm. Thank you!Amortizement
android:excludeFromRecents="true" works few minutes after idle. But after long idle, it can't recieves notification anymore. Please help!Armidaarmiger
@Armidaarmiger did you try this solution https://mcmap.net/q/822761/-background-service-is-not-restarting-after-killed-in-oppo-vivo-mi-android-version-7-1-2Stubble
M
0

These brands have a white list of apps that can survive normally. If your app not in white list os force stops any app that swiped at recents screen. You must tell user to add your app to whitelist manually.

Mesenchyme answered 4/4, 2020 at 12:17 Comment(3)
Whitelist comprises of auto-start permissions, exemption from battery optimization and permission of priority notofications. Tried all of these.Amortizement
how to add app to white list programatically?Armidaarmiger
@Armidaarmiger There is no way to add programmatically. You must contact with device vendors one by one to convince add your app to their whitelist. But they 99% will not accept that. You can use this lib to ask user to add your app to their device's whitelist: github.com/judemanutd/AutoStarterMesenchyme

© 2022 - 2024 — McMap. All rights reserved.