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

前端 未结 17 1032
借酒劲吻你
借酒劲吻你 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:40

    In Oreo Android defined limits to background services.

    To improve the user experience, Android 8.0 (API level 26) imposes limitations on what apps can do while running in the background.

    Still if you need always running service, then you can use foreground service.

    Background Service Limitations: While an app is idle, there are limits to its use of background services. This does not apply to foreground services, which are more noticeable to the user.

    So you can make a foreground service. You will need to show a notification to user when your service is running. See this answer (There are many others)

    A solution if -

    you don't want a notification for your service?

    You can make periodic task, 1. it starts your service, 2. service will do its work and 3. stops itself. By this your app will not be considered battery draining.

    You can use periodic task with Alarm Manager, Job Scheduler, Evernote-Jobs or Work Manager.

    • Work manager is best solution for periodic tasks. Which was introduced with Android Architecture Component.
    • Unlike Job-Scheduler(only >21 API) it will work for all versions.
    • Also it starts work after a Doze-Standby mode.
    • Make a Android Boot Receiver for scheduling service after device boot.

    I have tested forever running service with Work-Manager.

提交回复
热议问题