22

我正在尝试在某些操作发生后打开和关闭显示器(让我们暂时担心关闭屏幕)。根据我对唤醒锁的理解,这就是我所拥有的:

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

当我在 stackoverflow 和其他地方阅读其他帖子时,他们似乎告诉我 PARTIAL_WAKE_LOCK 会关闭屏幕。但是,如果我阅读 SDK,它会说它只允许关闭屏幕。我认为这是不对的。

4

5 回答 5

19

关闭屏幕有两种选择:

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);
于 2011-07-20T05:00:06.457 回答
10

以下内容是从SDK 文档中复制而来的。如果你想保持屏幕打开,我认为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

于 2011-07-20T03:59:23.180 回答
5

对我来说,这些方法不起作用。所以我使用了其他场景(不是微不足道的)来关闭我的屏幕。

Android 有 2 个标志负责唤醒:

  • 显示 --> 屏幕超时
  • 应用程序 --> 开发 -->充电时保持清醒复选框。

我使用了以下流程:

  1. 首先保存您之前的配置,例如屏幕超时为 1 分钟,并 选中充电时保持清醒。

  2. 之后,我取消选中充电时保持清醒并将屏幕超时设置为最短时间。

  3. 我注册广播接收器服务以从屏幕关闭的 android 获取事件。

  4. 当我在屏幕上关闭事件时,我将之前的配置设置为默认值:屏幕超时为 1 分钟,并 选中充电时保持清醒。

  5. 取消注册接收器

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

....

Android同步类

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
于 2012-10-16T20:50:10.160 回答
1

根据此链接,您也可以像这样关闭屏幕:

Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 1000);

1000 以毫秒为单位,表示 1 秒,您可以根据需要将其替换为任何值。

需要的许可:

<uses-permission android:name="android.permission.WRITE_SETTINGS" />
于 2015-08-17T15:25:28.590 回答
-2

尝试-wakeLock.acquire(1000); // 指定时间,它会变暗并最终关闭。

于 2014-03-26T06:49:06.523 回答