如何防止Android设备以编程方式休眠?

时间:2022-01-15 07:26:53

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

如何防止Android设备以编程方式休眠?

7 个解决方案

#1


122  

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 page that describes the different kinds of wakelocks.

这个页面上还有一个表格描述了不同种类的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.

注意,在使用唤醒锁时需要采取一些谨慎措施。确保在使用锁(或不在前台)时总是释放()锁。否则,你的应用程序可能会导致严重的电池消耗和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.

该文档还包含一个有用的页面,该页面描述了保持设备保持清醒的不同方法,以及您可能选择何时使用它们。如果“防止设备进入睡眠状态”只指屏幕(不让CPU处于活动状态),那么唤醒锁可能超出您的需要。

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

您还需要确保在清单中设置了WAKE_LOCK权限,以便使用此方法。

#2


204  

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.

如果您只是想在特定的视图上防止睡眠模式,只需在该视图上调用setKeepScreenOn(true),或者将keepScreenOn属性设置为true。这将防止视图在屏幕上时屏幕关闭。不需要特别许可。

#3


101  

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://*.com");
    }
}

After that, my app would not go to sleep while it was open. Thanks for the anwer goes to xSus.

在那之后,我的应用程序在打开的时候不会休眠。谢谢你的回答。

#4


17  

android:keepScreenOn="true" could be better option to have from layout XML.

在布局XML中,keepScreenOn=“true”可能是更好的选择。

More info: https://developer.android.com/training/scheduling/wakelock.html

更多信息:https://developer.android.com/training/scheduling/wakelock.html。

#5


5  

From the root shell (e.g. adb shell), you can lock with:

从根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)读写的,当然还有根。

A reference is here: http://lwn.net/Articles/479841/

这里有一个引用:http://lwn.net/Articles/479841/

#6


4  

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);
}

#7


3  

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 important that 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 example on 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)

我要给你们举一个很好的例子,关于如何使用警报管理器来触发事件并唤醒手机(你的应用)来执行你需要做的事情,然后再去睡觉:警报管理器(来源:commonsware.com)

#1


122  

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 page that describes the different kinds of wakelocks.

这个页面上还有一个表格描述了不同种类的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.

注意,在使用唤醒锁时需要采取一些谨慎措施。确保在使用锁(或不在前台)时总是释放()锁。否则,你的应用程序可能会导致严重的电池消耗和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.

该文档还包含一个有用的页面,该页面描述了保持设备保持清醒的不同方法,以及您可能选择何时使用它们。如果“防止设备进入睡眠状态”只指屏幕(不让CPU处于活动状态),那么唤醒锁可能超出您的需要。

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

您还需要确保在清单中设置了WAKE_LOCK权限,以便使用此方法。

#2


204  

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.

如果您只是想在特定的视图上防止睡眠模式,只需在该视图上调用setKeepScreenOn(true),或者将keepScreenOn属性设置为true。这将防止视图在屏幕上时屏幕关闭。不需要特别许可。

#3


101  

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://*.com");
    }
}

After that, my app would not go to sleep while it was open. Thanks for the anwer goes to xSus.

在那之后,我的应用程序在打开的时候不会休眠。谢谢你的回答。

#4


17  

android:keepScreenOn="true" could be better option to have from layout XML.

在布局XML中,keepScreenOn=“true”可能是更好的选择。

More info: https://developer.android.com/training/scheduling/wakelock.html

更多信息:https://developer.android.com/training/scheduling/wakelock.html。

#5


5  

From the root shell (e.g. adb shell), you can lock with:

从根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)读写的,当然还有根。

A reference is here: http://lwn.net/Articles/479841/

这里有一个引用:http://lwn.net/Articles/479841/

#6


4  

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);
}

#7


3  

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 important that 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 example on 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)

我要给你们举一个很好的例子,关于如何使用警报管理器来触发事件并唤醒手机(你的应用)来执行你需要做的事情,然后再去睡觉:警报管理器(来源:commonsware.com)