如何在 Oreo 中长时间运行后台服务? [英] How to run a background service in Oreo for longer period?

查看:31
本文介绍了如何在 Oreo 中长时间运行后台服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android Oreo 对后台服务的运行施加了很多限制.Oreo 中的服务现在不像以前那样正常.

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 to run a service in background for long periods of time.

我正在开发一个应用程序,用于在用户摇晃手机时启动手电筒.为此,我必须将 Sensor 侦听器代码放入服务中.

I am developing an application to launch the flashlight when user shakes the phone. To achieve this I will have to put the Sensor listener code inside a service.

如何防止安卓系统不终止服务.

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

PS:我不想用通知启动前台服务.

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

推荐答案

如何防止安卓系统不终止服务.

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

总结评论:使用前台服务,在专用频道上发出通知,频道设置为IMPORTANCE_DEFAULT.通知用户他们可以将该频道静音(例如,长按通知栏中的 Notification).使用专用频道意味着您仍然可以在其他频道上发出通知.您的通知也应该很有用:

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.

我不能排除 Android 8.x 中存在可被利用来提供无限期服务的某些错误的可能性.事实上,我认为很可能有什么东西漂浮在那里.但是,这显然违背了 Google 的意图,意思是:

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:

  • 在没有 Google 认为有效的理由的情况下利用该技术可能会导致您的应用在 Play 商店中被禁止(如果您计划以这种方式分发它)

  • 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

该错误可能会在未来的 Android 版本中修复,与 Google 进行军备竞赛往往是一个失败的提议

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

有足够多的空中手势"应用程序在浮动(即基于摇晃做事),理想情况下,Google 会为其添加一些专用的低功耗 API.例如,他们可以向 JobScheduler 添加功能以允许您注册震动事件并在这种情况下调用您的 JobService,就像他们允许您注册ContentProvider 中的更改.我不知道他们是否会提供这样的 API,但如果您愿意,您可以为其提交功能请求.

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.

这篇关于如何在 Oreo 中长时间运行后台服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆