Android 防止应用程序/屏幕超时

发布于 2024-07-23 10:21:33 字数 161 浏览 4 评论 0原文

我有一个 Android(版本 1.5)应用程序,按下按钮时需要不断运行。 因此,当按下 Button 时,我希望手机保持开机状态,不希望屏幕或 CPU 超时。

当按下另一个 Button 时,我希望手机恢复正常并根据用户设置超时。

I have an Android (version 1.5) application which needs to be constantly running when a button is pressed. So, when a Button is pressed I would like the phone to remain on and not want the screen or CPU to time-out.

When another Button is pressed I would like the phone to be back to normal and time-out as per user settings.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆追雨的时光 2024-07-30 10:21:33

更新:根据 Steve Pomeroy 的建议,可能是更好的方法。


可以使用需要的 WakeLock以下权限:

<uses-permission android:name="android.permission.WAKE_LOCK" />

以下是获取和释放 WakeLock

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
wl.acquire();
// wake locked...
wl.release();

根据您的要求,您可能可以使用不同类型的 唤醒锁

Update: As suggested by Steve Pomeroy, this might be a better way to do it.


You can use a WakeLock that requires the following permission:

<uses-permission android:name="android.permission.WAKE_LOCK" />

Here is how you aquire and release a WakeLock:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
wl.acquire();
// wake locked...
wl.release();

Depending on your requirements you might be able to use a different type of WakeLock.

眼波传意 2024-07-30 10:21:33

您应该考虑此处提出的解决方案,而不是使用唤醒锁: 强制屏幕打开

它更容易使用并且不会意外浪费用户的电池。

Instead of using a wakelock, you should consider the solution proposed here: Force Screen On

It is much easier to use and doesn't have the potential of accidentally wasting the user's batteries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文