Android 8.0: java.lang.IllegalStateException: Not allowed to start service Intent

前端 未结 17 1055
借酒劲吻你
借酒劲吻你 2020-11-22 09:15

On application launch, app starts the service that should to do some network task. After targeting API level 26, my application fails to start service on Android 8.0 on back

17条回答
  •  灰色年华
    2020-11-22 09:48

    The permitted situations are a temporary whitelist where the background service behaves the same as before 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.

    Source: https://developer.android.com/about/versions/oreo/background.html

    So in other words if your background service does not meet the whitelist requirements you have to use the new JobScheduler. It's basically the same as a background service, but it gets called periodically instead of running in the background continuously.

    If you're using an IntentService, you can change to a JobIntentService. See @kosev's answer below.

提交回复
热议问题