Chinese Android Devices Killing Background Service

丶灬走出姿态 提交于 2020-05-14 09:06:18

问题


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.


回答1:


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"




回答2:


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.



来源:https://stackoverflow.com/questions/61027722/chinese-android-devices-killing-background-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!