如何防止 Android 设备以编程方式进入睡眠状态?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3723634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I prevent an Android device from going to sleep programmatically?
提问by James
How do I prevent an Android device from going to sleep programmatically?
如何防止 Android 设备以编程方式进入睡眠状态?
采纳答案by eldarerathis
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();
There's also a table on this pagethat describes the different kinds of wakelocks.
此页面上还有一个表格,描述了不同类型的唤醒锁。
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.
请注意,使用唤醒锁时需要注意一些事项。确保release()
在完成后始终锁定(或不在前台)。否则,您的应用可能会导致严重的电池消耗和 CPU 使用。
The documentation also contains a useful pagethat 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.
该文档还包含一个有用的页面,该页面描述了保持设备唤醒的不同方法,以及何时可以选择使用一种方法。如果“防止设备进入睡眠状态”仅指屏幕(而不是保持 CPU 处于活动状态),那么唤醒锁定可能超出您的需要。
You also need to be sure you have the WAKE_LOCKpermission set in your manifest in order to use this method.
您还需要确保在清单中设置了WAKE_LOCK权限才能使用此方法。
回答by Witek
If you just want to prevent the sleep mode on a specific View
, just call setKeepScreenOn(true)
on that View
or set the keepScreenOn
property to true
. This will prevent the screen from going off while the View
is on the screen. No special permission required for this.
如果您只想阻止特定的睡眠模式View
,只需调用setKeepScreenOn(true)
它View
或将keepScreenOn
属性设置为true
. 这将防止屏幕在屏幕上显示时熄灭View
。这不需要特别许可。
回答by dogatonic
I found another working solution: add the following line to your app under the onCreate event.
我找到了另一个可行的解决方案:将以下行添加到您的应用程序的 onCreate 事件下。
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
My sample Cordova project looks like this:
我的示例 Cordova 项目如下所示:
package com.apps.demo;
import android.os.Bundle;
import android.view.WindowManager;
import org.apache.cordova.*;
public class ScanManActivity extends DroidGap {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
super.loadUrl("http://stackoverflow.com");
}
}
After that, my app would not go to sleep while it was open. Thanks for the anwer goes to xSus.
之后,我的应用程序在打开时不会进入睡眠状态。感谢 xSus 的回答。
回答by CoDe
android:keepScreenOn="true"
could be better option to have from layout XML.
android:keepScreenOn="true"
可能是布局 XML 的更好选择。
More info: https://developer.android.com/training/scheduling/wakelock.html
更多信息:https: //developer.android.com/training/scheduling/wakelock.html
回答by arjun
Set flags on Activity's Window as below
在活动窗口上设置标志,如下所示
@Override public void onResume() {
super.onResume();
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
@Override public void onPause() {
super.onPause();
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
回答by jelle foks
From the root shell (e.g. adb shell), you can lock with:
从 root shell(例如 adb shell),您可以锁定:
echo mylockname >/sys/power/wake_lock
After which the device will stay awake, until you do:
之后设备将保持唤醒状态,直到您执行以下操作:
echo mylockname >/sys/power/wake_unlock
With the same string for 'mylockname'.
'mylockname' 使用相同的字符串。
Note that this will not prevent the screen from going black, but it will prevent the CPU from sleeping.
请注意,这不会阻止屏幕变黑,但会阻止 CPU 休眠。
Note that /sys/power/wake_lock is read-write for user radio (1001) and group system (1000), and, of course, root.
请注意,/sys/power/wake_lock 是用户无线电 (1001) 和组系统 (1000) 的读写,当然还有 root。
A reference is here: http://lwn.net/Articles/479841/
参考在这里:http: //lwn.net/Articles/479841/
回答by lucabox
what @eldarerathis said is correct in all aspects, the wake lock is the right way of keeping the device from going to sleep.
@eldarerathis 所说的在所有方面都是正确的,唤醒锁是防止设备进入睡眠状态的正确方法。
I don't know waht you app needs to do but it is really importantthat you think on how architect your app so that you don't force the phone to stay awake for more that you need, or the battery life will suffer enormously.
我不知道你的应用程序需要做什么,但考虑如何构建你的应用程序真的很重要,这样你就不会强迫手机保持唤醒状态以获得更多你需要的东西,否则电池寿命会受到极大的影响。
I would point you to this really good exampleon how to use AlarmManager
to fire events and wake up the phone and (your app) to perform what you need to do and then go to sleep again: Alarm Manager (source: commonsware.com)
我会向您指出这个非常好的示例,了解如何使用AlarmManager
触发事件和唤醒电话和(您的应用程序)执行您需要做的事情然后再次进入睡眠状态:警报管理器(来源:commonsware.com)
回答by hogarth45
If you are a Xamarin user, this is the solution:
如果您是 Xamarin 用户,这是解决方案:
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle); //always call superclass first
this.Window.AddFlags(WindowManagerFlags.KeepScreenOn);
LoadApplication(new App());
}