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

查看:76
本文介绍了如何在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.

如何防止android系统不终止该服务.

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.

推荐答案

如何防止android系统不终止该服务.

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天全站免登陆