How to run a background service in Oreo for longer period?

后端 未结 4 724
滥情空心
滥情空心 2020-12-03 15:39

Android Oreo has imposed many restrictions on running background service. Services now don\'t behave like normal in Oreo as they used to before.

But what if I have t

4条回答
  •  [愿得一人]
    2020-12-03 16:06

    How do I prevent android system to not kill the service.

    To summarize the comments: Use a foreground service, with a notification on a dedicated channel, with the channel set to IMPORTANCE_DEFAULT. Advise the user that they can mute that channel (e.g., long-press on the Notification in the notification shade). Using a dedicated channel means that you can still raise notifications on other channels. Your notification should also be useful:

    • Have a "stop" action to stop your service, if the user wants to shut it down for a while

    • Tapping on the notification itself would lead to your activity for configuring your app's behavior

    I don't want to start a foreground service with a notification.

    Then most likely you cannot write your app.

    I cannot rule out the possibility of some bug in Android 8.x that could be exploited to have an indefinite-duration service. In fact, I'd consider it to be fairly likely that there's something floating around out there. However, this is clearly against Google intentions, meaning:

    • Exploiting that technique, without what Google would consider to be valid justification, might get your app banned from the Play Store, if that was how you planned to distribute it

    • The bug might be fixed in a future version of Android, and getting in an arms race with Google tends to be a losing proposition

    There are enough "air gesture" apps floating about (i.e., do things based on a shake) that, ideally, Google would add some dedicated low-power API for it. For example, they could add functionality to JobScheduler to allow you to register for a shake event and have your JobService be invoked in that circumstance, just as they allow you to register for changes in a ContentProvider. I have no idea whether they will ever offer such an API, but you could file a feature request for it, if you wanted.

提交回复
热议问题