Android Kills Background Services in Xiaomi, Huawei etc

霸气de小男生 提交于 2019-12-02 06:50:28

问题


I need to make my service unstoppable. I tried to return START_STICKY on start command, it works well on emulator but, when remove the app from task manager on my device (Xiaomi mi5s, Android 7), it doesn't create itself again.

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    return START_STICKY;
}

Edit: I gave Auto Start permission and disabled battery protection rules for the app.

Edit: Also I tried to use foreground service. However, it is being killed too..

   @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);

        Intent notificationIntent = new Intent(getApplicationContext(), StartActivity.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent contentPendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle(getResources().getString(R.string.app_name))
                .setTicker(getResources().getString(R.string.app_name))
                .setContentText("Hello!")
                .setSmallIcon(R.mipmap.ic_launcher2)
                .setContentIntent(contentPendingIntent)
                .setOngoing(true)
                .build();
        notification.flags = notification.flags | Notification.FLAG_NO_CLEAR;
        startForeground(1, notification);

        return START_NOT_STICKY;
    }


---Activity
       ContextCompat.startForegroundService(this, new Intent(this, LockerService.class));

Logcat

2018-10-31 21:24:22.143 2168-2168/? D/wpa_supplicant: wlan0: Control interface command 'SIGNAL_POLL'
2018-10-31 21:24:22.555 1513-3093/? D/ProcessManager: update DY:[com.touchtype.swiftkey, com.miui.voip]
2018-10-31 21:24:22.560 1513-3093/? D/ProcessManager: remove task: TaskRecord{36f7d55 #495 A=me.ibrahimsn.applock U=0 StackId=1 sz=1}
2018-10-31 21:24:22.607 3103-3296/? I/WtEventController: dispatchSysInfoEvent AM_NEW_INTENT componentName:com.miui.home/.launcher.Launcher
2018-10-31 21:24:22.610 1513-9074/? D/ActivityTrigger: ActivityTrigger activityPauseTrigger 
2018-10-31 21:24:22.624 1513-9074/? I/Timeline: Timeline: App_transition_ready time:44879245
2018-10-31 21:24:22.626 1513-1604/? D/ActivityTrigger: ActivityTrigger activityStopTrigger 
2018-10-31 21:24:22.628 1513-1620/? I/Timeline: Timeline: App_transition_ready time:44879248
2018-10-31 21:24:22.630 1513-12578/? I/Timeline: Timeline: App_transition_ready time:44879251
2018-10-31 21:24:22.633 3185-3185/? D/SearchCardView: onVisibilityChanged: 0
2018-10-31 21:24:22.635 3185-3831/? I/RenderThread: RenderThread resumed
2018-10-31 21:24:22.635 3185-3831/? D/ScreenElementRoot: resume
2018-10-31 21:24:22.639 3185-3831/? I/NotifierManager: onRegister: miui.maml.NotifierManager$MultiBroadcastNotifier@46712e7
2018-10-31 21:24:22.646 1513-3093/? I/Timeline: Timeline: App_transition_ready time:44879267
2018-10-31 21:24:22.680 1513-1620/? I/Timeline: Timeline: App_transition_ready time:44879301
2018-10-31 21:24:22.686 2134-2134/? V/PhoneStatusBarPolicy: updateManagedProfile mManagedProfileFocused: false mManagedProfileInQuietMode: false mKeyguardVisible: false mCurrentUserId:0 mCurrentProfileId:0 mSecondSpaceStatusIconVisible: true showIcon:false
2018-10-31 21:24:22.689 1513-10970/? D/GraphicsStats: Buffer count: 4
2018-10-31 21:24:22.694 2134-2134/? D/StatusBar: onNotificationRemoved:  Key: 0|me.ibrahimsn.applock|1|null|10637

How can i do that?


回答1:


There are some customizations on Chinese roms regarding app lifecycle.

Xiaomi kills foreground services indiscriminately (xiaomi redmi note4 (Android N), note 7 (Android P)). So it's really difficult to have the same behavihour on all phones.

You can try to relaunch your foreground service using an alarm with a broadcast receiver. When you app is being killed you need to set this alarm. When the intent is received try to start the foreground service with startForeground(). Remember also to bind the foreground service and disable battery limitation (and others things related) according to your device.

I don't know if this works because we have no necessity to implement such a method. But you could give it a try.

Remember that for Android >= Oreo the only way for a service to survive and works seems to be the foreground service way.(Bound service with battery optimization off for some devices)

You can find useful material at:

Never Ending background service

LocationUpdatesForegroundService

The second example works on emulator on different version of android(5/7). It works on Galaxy S3 with Resurrection Remix rom (Android 7) and works on some Huawei models (Oreo, Pie).

Work with services in android is really difficult. Have patience and go on.



来源:https://stackoverflow.com/questions/53084293/android-kills-background-services-in-xiaomi-huawei-etc

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