我试图在某个动作发生后打开和关闭显示器(让我们担心暂时关闭屏幕)。根据我对唤醒锁的理解,这就是我所拥有的:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
当我在stackoverflow上阅读其他帖子时,他们似乎告诉我PARTIAL_WAKE_LOCK将关闭屏幕。但是,如果我阅读SDK,它会说它只允许屏幕关闭。所以我觉得这不对。
任何提示都会有所帮助! 谢谢,
麦克
答案 0 :(得分:16)
关闭屏幕有两种选择:
PowerManager manager = (PowerManager) getSystemService(Context.POWER_SERVICE);
// Choice 1
manager.goToSleep(int amountOfTime);
// Choice 2
PowerManager.WakeLock wl = manager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Your Tag");
wl.acquire();
wl.release();
您可能也需要此权限:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<强>更新强>
尝试这种方法;一旦光线足够低,android就会关闭屏幕。
WindowManager.LayoutParams params = getWindow().getAttributes();
params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
params.screenBrightness = 0;
getWindow().setAttributes(params);
答案 1 :(得分:7)
以下内容已从SDK document复制。
如果你想保持屏幕,我认为SCREEN_BRIGHT_WAKE_LOCK
已经足够了。
Flag Value CPU Screen Keyboard
PARTIAL_WAKE_LOCK On* Off Off
SCREEN_DIM_WAKE_LOCK On Dim Off
SCREEN_BRIGHT_WAKE_LOCK On Bright Off
FULL_WAKE_LOCK On Bright Bright
答案 2 :(得分:4)
对我来说,这些方法不起作用。所以我使用其他场景(不是微不足道的)来关闭我的屏幕。
Android有2个标志负责醒着:
我使用了跟随流程:
首先保存以前的配置,例如屏幕超时为1分钟,在充电时保持清醒。
之后,我取消选中在充电时保持清醒并将屏幕超时设置为最短时间。
我注册广播接收服务,从屏幕关闭的android获取事件。
当我关闭屏幕上的事件时,我将先前的配置设置为默认值:屏幕超时为1分钟且在充电时保持清醒状态。
取消注册接收器
15秒后设备睡觉
以下是代码片段:
<强>广播接收器强>
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
/**
* Catch Screen On/Off
* */
public class BroadcastReceiverScreenListener extends BroadcastReceiver{
private BroadCastListenerCallBackItf mBroadCastListenerCallBack = null;
public BroadcastReceiverScreenListener(
BroadCastListenerCallBackItf broadCastListenerCallBack) {
this.mBroadCastListenerCallBack = broadCastListenerCallBack;
}
@Override
public void onReceive(Context arg0, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
mBroadCastListenerCallBack.broadCastListenerCallBack__ScreenOff_onResponse();
}
}
}
用作回调的界面
public interface BroadCastListenerCallBackItf {
public void broadCastListenerCallBack__ScreenOff_onResponse();
}
主要课程中的2种方法:
....
AndroidSynchronize mSync = new AndroidSynchronize();
....
public void turnScreenOff(int wait){
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadCastListenerCallBackItf broadCastListenerCallBack = this;
BroadcastReceiver mReceiver = new BroadcastReceiverScreenListener(broadCastListenerCallBack);
m_context.registerReceiver(mReceiver, filter);
//set Development --> disable STAY_ON_WHILE_PLUGGED_IN
Settings.System.putInt(
m_context.getContentResolver(),
Settings.System.STAY_ON_WHILE_PLUGGED_IN,
0 );
// take current screen off time
int defTimeOut = Settings.System.getInt(m_context.getContentResolver(),
Settings.System.SCREEN_OFF_TIMEOUT, 3000);
// set 15 sec
Settings.System.putInt(m_context.getContentResolver(),
Settings.System.SCREEN_OFF_TIMEOUT, 15000);
// wait 200 sec till get response from BroadcastReceiver on Screen Off
mSync.doWait(wait*1000);
// set previous settings
Settings.System.putInt(m_context.getContentResolver(),
Settings.System.SCREEN_OFF_TIMEOUT, defTimeOut);
// switch back previous state
Settings.System.putInt(
m_context.getContentResolver(),
Settings.System.STAY_ON_WHILE_PLUGGED_IN,
BatteryManager.BATTERY_PLUGGED_USB);
m_context.unregisterReceiver(mReceiver);
}
public void broadCastListenerCallBack__ScreenOff_onResponse() {
mSync.doNotify();
}
....
AndroidSynchronize类
public class AndroidSynchronize {
public void doWait(long l){
synchronized(this){
try {
this.wait(l);
} catch(InterruptedException e) {
}
}
}
public void doNotify() {
synchronized(this) {
this.notify();
}
}
public void doWait() {
synchronized(this){
try {
this.wait();
} catch(InterruptedException e) {
}
}
}
}
<强> [编辑] 强>
您需要注册权限:
android.permission.WRITE_SETTINGS
答案 3 :(得分:1)
根据这个link,你也可以关闭屏幕:
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 1000);
1000以毫秒为单位,这意味着1秒,您可以根据需要将其替换为任何值。
需要许可:
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
答案 4 :(得分:-2)