【问题标题】:Android Kills Background Services in Xiaomi, Huawei etcAndroid杀死小米、华为等后台服务
【发布时间】:2018-10-31 13:17:01
【问题描述】:

我需要让我的服务不可阻挡。我尝试在启动命令上返回 START_STICKY,它在模拟器上运行良好,但是当从我的设备(小米 mi5s,Android 7)上的任务管理器中删除应用程序时,它不会再次创建自己。

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

    return START_STICKY;
}

编辑:我为应用授予了自动启动权限并禁用了电池保护规则。

编辑:我也尝试使用前台服务。但是,它也被杀死了..

   @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

我该怎么做?

【问题讨论】:

  • 从最近删除应用程序会杀死他们的进程,包括服务。除非有广播/事件重新启动它们,否则它们不会被重新创建。
  • 但它在模拟器和其他设备上运行良好。 START_STICKY 用于在销毁后重新启动服务。 @Pawel
  • 它也停止了 :( @greeble31​​span>
  • @mystogan 在这种情况下,您可能想提出一个新问题,或者研究一些有关前台服务的教程。它们是解决您所描述的问题的有据可查的方法。

标签: android background-service huawei-mobile-services xiaomi


【解决方案1】:

请看here。我遇到了同样的问题,经过大量研究,我想通了:您必须将您的应用列入白名单。

【讨论】:

    【解决方案2】:

    中文rom有一些关于应用生命周期的自定义。

    小米乱杀前台服务(xiaomi redmi note4 (Android N), note 7 (Android P))。所以很难在所有手机上都有相同的行为。

    您可以尝试使用带有广播接收器的警报重新启动前台服务。 当您的应用程序被杀死时,您需要设置此警报。当收到意图时,尝试使用 startForeground() 启动前台服务。还记得根据您的设备绑定前台服务并禁用电池限制(和其他相关的东西)。

    我不知道这是否可行,因为我们没有必要实现这样的方法。不过你可以试一试。

    请记住,对于 Android >= Oreo,服务生存和工作的唯一方式似乎是前台服务方式。(绑定服务对某些设备关闭电池优化)

    您可以在以下位置找到有用的材料:

    Never Ending background service

    LocationUpdatesForegroundService

    第二个示例适用于不同版本的 android(5/7) 上的模拟器。它适用于配备 Resurrection Remix rom (Android 7) 的 Galaxy S3,适用于某些华为机型(奥利奥、派)。

    在 android 中使用服务真的很困难。有耐心,继续。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-26
      • 2014-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多