【问题标题】:How to run infinite service on Android (Foreground service killed)?如何在 Android 上运行无限服务(前台服务被杀死)?
【发布时间】:2019-06-21 22:56:17
【问题描述】:

我创建了一个粘性前台服务,每次它被杀死时都会重新启动,它似乎工作正常,但我注意到在某些情况下(当操作系统杀死它时)它不会重新启动。每次它被杀死时,我怎样才能让它重新启动?我希望此服务始终运行。

喜欢那些服务。

这是我的前台服务 onStartCommand:

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

    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,
            0, notificationIntent, 0);

    Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle("InsiteMobile Service")
            .setContentText("Run the app by clicking here")
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentIntent(pendingIntent)
            .build();
    Log.i("InsiteMobileLog", "Service Started");
    startForeground(1, notification);

    //do heavy work on a background thread
    //stopSelf();

    return START_STICKY;
}

这就是我在 MainActivity onCreate() 中调用服务的方式:

public void startService() {

    Intent serviceIntent = new Intent(this, AppService.class);
    serviceIntent.putExtra("inputExtra", "InsiteMobileService");

    ContextCompat.startForegroundService(this, serviceIntent);
}

【问题讨论】:

  • 幸运的是你不能这样做 - 我真的不希望有一个“不朽”的恶意服务
  • 为什么我不能,而 WhatsApp、Facebook 和所有这些都可以?顺便说一句,它是一个私人应用程序供个人使用,这就是我需要的。
  • 它是否用于个人用途并不重要:禁止此类服务 - 如果您需要“长期”服务,请在前台启动它们(Service#startForeground() 方法)
  • 我写道我确实使用了 startForeground()。但在某些情况下,此服务会被操作系统杀死,在这种情况下不会重新启动。
  • 你能显示你的前台代码吗?前台服务不是操作系统在资源不足时终止的候选者。

标签: android android-service


【解决方案1】:

目前我正在处理需要在后台运行服务的同一个项目。我们需要注意几件事
1.在服务的onDestroy()方法中,使用startService Intent
重新启动自己 2.对于kitkat及以下的操作系统,在onStartCommand()方法中使用onTaskRemoved(intent);
3.在onStartCommand()中返回START_STICKY

在此之后,您需要了解一件事,设备的电话管理器往往会根据电池优化策略杀死后台进程。 检查测试设备的电话管理器设置,然后重试。

另外,如果您要使用 notification 来保持服务处于活动状态,则需要重新检查通过您的应用程序发送通知的权限。

【讨论】:

  • 你能再解释一下第2个吗?我无法理解。您能否更具体地了解一下电话管理器的事情?
  • START_STICKY 不适用于 Android kitkat 及以下版本,因此我们必须使用 onTaskRemove() 使服务在尽可能多的设备的后台运行。
  • 而且,几乎所有的设备都有手机管理器,确保手机运行顺畅。为此,它会杀死后台服务以及系统中空闲一段时间的应用程序。因此,在测试您的应用时,只需寻找电话管理器设置/电池优化设置,通过编码来解决它们。
【解决方案2】:

Android Oreo 有一些关于服务的limitations。你可以看看这个线程https://stackoverflow.com/a/47611385/4082934。此外,Evernote 团队对与 Android Oreo(API 级别 26)兼容的后台服务有很好的libraryJobScheduler 库通过 JobInfo.Builder.setPeriodic(long intervalMillis, long flexMillis) 方法提供周期性服务。此服务的最短工作时间为 15 分钟。一些前台服务教程:

  1. https://blog.klinkerapps.com/android-o-background-services/
  2. https://medium.com/exploring-android/exploring-background-execution-limits-on-android-oreo-ab384762a66c

【讨论】:

    【解决方案3】:

    您无法创建始终在最新操作系统版本上运行的服务。但是你可以使用ForegroundService with notification,这不能被杀死。原则是用户应该始终看到您的应用程序正在运行并消耗操作系统资源。

    【讨论】:

    • 我写道我确实使用了 startForeground()。但在某些情况下,此服务会被操作系统杀死,在这种情况下不会重新启动。
    【解决方案4】:

    我也在做一个项目,需要一个不朽的服务,所以你应该为此目的使用 Job Scheduler。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-30
      • 1970-01-01
      • 2012-08-26
      • 1970-01-01
      • 2021-07-02
      • 1970-01-01
      • 2022-12-16
      相关资源
      最近更新 更多