我如何prevent Android设备进入休眠状态编程? [英] How do I prevent an Android device from going to sleep programmatically?

查看:99
本文介绍了我如何prevent Android设备进入休眠状态编程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何prevent Android设备进入休眠状态编程?

How do I prevent an Android device from going to sleep programmatically?

推荐答案

一种选择是使用的唤醒锁。从文档示例:

One option is to use a wake lock. Example from the docs:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
wl.acquire();

// screen and CPU will stay awake during this section

wl.release();

还有在此页面描述不同种类的wakelocks。

There's also a table on this page that describes the different kinds of wakelocks.

请注意,某些需要谨慎使用唤醒锁时,必须考虑。确保你总是发布()当你用它做(或没有在前台)的锁。否则,你的应用程序可能会导致一些严重的电池消耗和CPU使用率。

Be aware that some caution needs to be taken when using wake locks. Ensure that you always release() the lock when you're done with it (or not in the foreground). Otherwise your app can potentially cause some serious battery drain and CPU usage.

该文件还包含一个有用的页面,描述不同的方法来保持设备清醒,而当你可以选择使用的。如果prevent设备进入休眠状态只是指的屏幕的(而不是保持CPU活跃),则唤醒锁可能是比你更需要。

The documentation also contains a useful page that describes different approaches to keeping a device awake, and when you might choose to use one. If "prevent device from going to sleep" only refers to the screen (and not keeping the CPU active) then a wake lock is probably more than you need.

您还需要确保您有 WAKE_LOCK 权限设置您的清单,才能使用此方法。

You also need to be sure you have the WAKE_LOCK permission set in your manifest in order to use this method.

这篇关于我如何prevent Android设备进入休眠状态编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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