在 Android 上关闭屏幕

发布于 2024-11-25 07:35:04 字数 350 浏览 1 评论 0 原文

我试图在某个操作发生后打开和关闭显示器(我们现在只关心关闭屏幕)。根据我对唤醒锁的理解,这就是我所拥有的:

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

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

I am trying to turn on and off the display after a certain action happens (Lets just worry about turning the screen off for now). From what I understand from wake lock, this is what I have:

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

When I read other posts on stackoverflow and else where, they seem to tell me that PARTIAL_WAKE_LOCK will turn the screen off. But if I read the SDK it says that it will only allow the screen to be turned off. I think this isn't right.

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

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

发布评论

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

评论(5

万人眼中万个我 2024-12-02 07:35:04

关闭屏幕有两种选择:

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

There are two choices for turning the screen off:

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

You will probably need this permission too:

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

UPDATE:

Try this method; android turns off the screen once the light level is low enough.

WindowManager.LayoutParams params = getWindow().getAttributes();
params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
params.screenBrightness = 0;
getWindow().setAttributes(params);
久伴你 2024-12-02 07:35:04

以下内容复制自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

The following is copied from SDK document.
If you want to keep screen on, I think SCREEN_BRIGHT_WAKE_LOCK is enough.


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

淡淡離愁欲言轉身 2024-12-02 07:35:04

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

Android 有 2 个负责唤醒的标志:

  • Display -->屏幕超时
  • 应用程序 -->发展历程--> 充电时保持清醒复选框。

我使用了以下流程:

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

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

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

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

  5. 取消注册接收器

15 秒后。设备睡眠

以下是代码片段:

BroadcastReceiver

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

< strong>[编辑]

您需要注册权限:

android.permission.WRITE_SETTINGS

For me those methods didn't work. So I used other scenario (not trivial) to make my screen off.

Android has 2 flags that responsible to be awake:

  • Display --> Screen TimeOut
  • Application --> Development --> Stay awake while charging check box.

I used followed flow:

  1. 1st of all save your previous configuration, for example screen timeout was 1 min and Stay awake while charging checked.

  2. After, I uncheck Stay awake while charging and set screen timeout to minimal time.

  3. I register to broadcast receiver service to get event from android that screen turned off.

  4. When I got event on screen off, I set previous configuration to default: screen timeout was 1 min and Stay awake while charging checked.

  5. Unregister receiver

After 15 sec. device sleeps

Here is snippets of code:

BroadcastReceiver

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

}

Interface used as callback

public interface BroadCastListenerCallBackItf {
    public void broadCastListenerCallBack__ScreenOff_onResponse();
}

2 methods from main class:

....

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 class

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

[EDIT]

You need to register permission:

android.permission.WRITE_SETTINGS
浸婚纱 2024-12-02 07:35:04

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

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

1000 是以毫秒为单位,这意味着 1 秒,您可以根据需要将其替换为任何值。

需要许可:

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

Per this link, You can also turn the screen off like this:

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

1000 is in milliseconds which means 1 second, you can replace it with any value as desired.

Needed permission:

<uses-permission android:name="android.permission.WRITE_SETTINGS" />
绮筵 2024-12-02 07:35:04

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

try -
wakeLock.acquire(1000); // specify the time , it dims out and eventually turns off.

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