Android 8.0:java.lang.IllegalStateException:不允许启动服务Intent

在针对API级别26后,我的应用程序无法在后台启动Android 8.0上的服务.

Caused by: java.lang.IllegalStateException: Not allowed to start
service Intent {
}: app is in background uid UidRecord{90372b1 u0a136 CEM idle procs:1

Background execution limits

The startService() method now throws an IllegalStateException if an
app targeting Android 8.0 tries to use that method in a situation when
it isn’t permitted to create background services.

“在不允许的情况下” – 它实际意味着什么?以及如何解决它.我不想把我的服务设置为“前景”

允许的情况是临时白名单,其中后台服务的行为与Android O之前相同.

Under certain circumstances, a background app is placed on a temporary whitelist for several minutes. While an app is on the whitelist, it can launch services without limitation, and its background services are permitted to run. An app is placed on the whitelist when it handles a task that’s visible to the user, such as:

  • Handling a high-priority Firebase Cloud Messaging (FCM) message.
  • Receiving a broadcast, such as an SMS/MMS message.
  • Executing a PendingIntent from a notification.
  • Starting a VpnService before the VPN app promotes itself to the foreground.



如果您使用的是IntentService,则可以更改为JobIntentService.见@ kosev的answer below.

转载注明原文:Android 8.0:java.lang.IllegalStateException:不允许启动服务Intent - 代码日志