屏幕关闭/锁定后手机振动停止工作 [英] Phone vibration stops working after screen is off / locked

查看:86
本文介绍了屏幕关闭/锁定后手机振动停止工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一项服务可以检查我网站上的更新,我希望它能够在发现更新时引起振动,即使屏幕关闭或锁定.

I have a service that checks for updates on my website and I would like it to be able to cause a vibration when a update is found even if the screen is off or locked.

目前仅当屏幕未关闭/锁定时振动才有效.即使屏幕关闭/锁定,所有其他功能也能正常工作.

The vibration currently only works if the screen is not off / locked. All other functions does work even if the screen is off / locked.

Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
   long[] pattern = new long[]{0, 400, 200, 400};
   if (Build.VERSION.SDK_INT >= 26) { // New API
     vibrator.vibrate(VibrationEffect.createWaveform(pattern,0));
   } else { // Old API
     vibrator.vibrate(pattern, 0);
}

如何在屏幕关闭的情况下振动手机?我试过使用 WakeLock 但这似乎不是问题?

How can I vibrate a phone even if its screen is off? I've tried to use a WakeLock but that doesn't seem to be the problem?

我设置了所有权限,因为它在屏幕打开时工作.

I have all the permissions set, as its working when the screen is on.

推荐答案

鉴于此:

我有一项服务可以检查我网站上的更新,我希望它能够在发现更新时引起振动,即使屏幕关闭或锁定.

I have a service that checks for updates on my website and I would like it to be able to cause a vibration when a update is found even if the screen is off or locked.

还有这个:

如何在屏幕关闭的情况下振动手机?我试过使用 WakeLock 但这似乎不是问题?

How can I vibrate a phone even if its screen is off? I've tried to use a WakeLock but that doesn't seem to be the problem?

我得出的结论是,当您的应用在前台时,您的服务可以正常工作,但在后台时则不然.因此,您的目标 Android API 很可能是 26 及更高版本.

I draw a conclusion that your service works without problems when your app is in the foreground but not when it's in the background. So, it's most likely your target Android API is 26 and higher.

在这种情况下,由于操作系统施加的后台限制,您的更新服务会在一段时间后停止工作.由于更新服务不再运行,显然它无法启动任何振动.

In this case, your update service stops working after some time due to the background limitations imposed by the OS. As the update service no longer runs, obviously, it cannot initiate any vibration.

从 Android 8.0 开始,对于在后台运行的应用程序有限制.因此,您的服务将工作一段时间,然后如文档中所述被系统终止(请阅读更多此处):

Starting from Android 8.0 there are limitations on what apps can run when they're in the background. So, your service will work for a certain time and then it will be killed by the system as stated in the documentation (please read more here):

当一个应用程序进入后台时,它有几个窗口仍然允许创建和使用服务的分钟数.在该窗口结束时,应用程序被认为是空闲的.在这时间,系统停止应用的后台服务,就像应用调用了服务的 Service.stopSelf() 方法.

When an app goes into the background, it has a window of several minutes in which it is still allowed to create and use services. At the end of that window, the app is considered to be idle. At this time, the system stops the app's background services, just as if the app had called the services' Service.stopSelf() methods.

因此,如果您希望您的服务在应用程序处于后台时继续运行,您仍然可以选择.其中一些如下

Hence, if you want your service to continue operating while the app is in the background you still have options. Some of them are the following

WorkManager API 可以轻松安排可延迟的异步即使应用程序退出或设备预期也会运行的任务重新启动.

The WorkManager API makes it easy to schedule deferrable, asynchronous tasks that are expected to run even if the app exits or device restarts.

将您的服务替换为 WorkManager,它会定期运行以进行查询你的服务器.

Replace your service with a WorkManager, which is run periodically to query your server.

好吧,如果您使用 WorkManager,您实际上并不需要它.如果您的设备 API 是 23 及更高版本,它将被 WorkManager 内部使用.

Well, you don't really need it if you use WorkManager. It will be used by WorkManager internally if your device API is 23 and later.

将您的服务转换为前台服务,以便应用在执行期间被视为在前台.但在这种情况下,它必须在您执行更新检查的整个过程中都处于活动状态,因此它可能不是最佳解决方案.请在此处阅读更多内容.

Convert your service to a foreground service so that the app is considered in foreground during its execution. But in this case, it has to be active the whole time you're performing your update checks, so maybe it's not the best solution. Please read more here.

请记住,从 API 26 开始,应将后台服务替换为 WorkManagerForeground Service.否则,它们可能无法按预期执行,因为系统会在一定时间后杀死它们.

Keep in mind that background services should be replaced with WorkManager or Foreground Service starting from API 26. Otherwise, they may not perform as intended as the system kills them after a certain amount of time.

这篇关于屏幕关闭/锁定后手机振动停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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