认识音量按钮presses当屏幕关闭的Andr​​oid [英] Recognize volume button presses when screen is off Android

查看:274
本文介绍了认识音量按钮presses当屏幕关闭的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想承认,当用户presses音量的摇滚乐队,当屏幕处于关闭状态更新活动。从我读过,BroadcastReceivers(我认为)不工作的时候,手机是睡着了,所以要做到这一点的唯一方法就是保持活动使用PARTIAL_WAKE_LOCK运行。我的应用程序是一个基本的,不应该用太多的电池,但我担心使用PARTIAL_WAKE_LOCK可能会耗尽电池(其中违抗识别按钮presses当屏幕关闭的目的)。

I am trying to recognize when the user presses the volume rockers when the screen is off to update an activity. From what I've read, BroadcastReceivers (I think) don't work when the phone is asleep, and so the only way to do this is to keep the activity running using a PARTIAL_WAKE_LOCK. My app is a basic one that shouldn't use too much battery, but I'm concerned that using a PARTIAL_WAKE_LOCK might drain the battery (which defies the purpose of recognizing button presses when the screen is off).

请问PARTIAL_WAKE_LOCK实际上是一个问题,一个基本的应用程序吗?如果是这样,什么是去这样做的最佳方式,如果没有,什么是使用PARTIAL_WAKE_LOCK(即当我应该获得/释放它)?

Would the PARTIAL_WAKE_LOCK actually be an issue for a basic app? If so, what is the best way to go about doing this, and if not, what is the best way to use the PARTIAL_WAKE_LOCK (i.e. when should I acquire/release it)?

在此先感谢。

推荐答案

有两个问题;我打算把重点放在它的WAKELOCK部分。

There's two questions here; I'm going to focus on the "WAKELOCK' portion of it.

唤醒锁是臭名昭著的通过移动设备的电池翻腾。记者了解到,该设备将屏幕变暗,然后关闭屏幕关闭CPU和进入睡眠模式之前。这是为您的手机一个非常重要的一步,因为它可以节省电池。

Wake Locks are notorious for churning through the battery of a mobile device. Understand that the device will dim the screen, then turn off the screen before turning off the CPU and entering "sleep" mode. This is an extremely important step for your phone, since it conserves battery.

唤醒锁是非常危险的,因为它太容易忘记释放锁时,你就大功告成了。

Wake locks are extremely dangerous, because it's too easy to forget to release the lock when you're done.

在一般情况下,你不应该试图保持当它想睡觉设备清醒;这会激怒你的用户,你在吃了他们的电池。

In general, you shouldn't try to keep the device awake when it's trying to sleep; This will infuriate your users as you're eating up their battery.

下面是一些提示,说不定处理唤醒锁以更好的方式:

Here's some tips to maybe handle wake locks in a better way:

1)仅允许该操作,如果用户是停靠/充电

1) Only allow this operation if the user is docked/ charging

2)使用WakeLock.aquire(..),它接受一个超时参数的版本。通过这种方式,可以唤醒,检查一些国家,然后回去睡觉,如果没有自动发生。 (见 http://goo.gl/FkrO8

2) Use the version of WakeLock.aquire(..) that accepts a timeout parameter. This way, you can wake up, check for some states, and then go back to sleep automatically if nothing is happening. (see http://goo.gl/FkrO8)

3)使用AlarmManager创造-精确计时器;这将使您的应用程序被唤醒时,其他应用程序都意识到了要做的工作,以及。

3) Use AlarmManager to create in-exact timers; This will allow your app to be woken up when other apps have woken up to do work as well.

4)使用的jobscheduler API(发布于L),这将使你处理这种类型的调度和工作管理延迟,从一个单一的API。 (见 http://goo.gl/Z1AqSn

4) Use the JobScheduler API (released in L) which will allow you to handle this type of scheduling, and work deferring, from a single API. (see http://goo.gl/Z1AqSn)

这篇关于认识音量按钮presses当屏幕关闭的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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