猿问

在Android上关闭屏幕

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


PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);    

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

当我在stackoverflow上阅读其他帖子时,他们似乎告诉我PARTIAL_WAKE_LOCK将关闭屏幕。但是,如果我阅读SDK,它会说它只允许屏幕关闭。所以我觉得这不对。


任何提示都会有所帮助!谢谢,


千万里不及你
浏览 928回答 3
3回答

Cats萌萌

关闭屏幕有两种选择:PowerManager manager = (PowerManager) getSystemService(Context.POWER_SERVICE);// Choice 1manager.goToSleep(int amountOfTime);// Choice 2PowerManager.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);

元芳怎么了

以下内容从SDK文档中复制。如果你想保持屏幕,我认为SCREEN_BRIGHT_WAKE_LOCK就足够了。Flag Value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CPU&nbsp; &nbsp;Screen&nbsp; KeyboardPARTIAL_WAKE_LOCK&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; On*&nbsp; &nbsp; Off&nbsp; &nbsp; &nbsp; OffSCREEN_DIM_WAKE_LOCK&nbsp; &nbsp; &nbsp; &nbsp;On&nbsp; &nbsp; &nbsp;Dim&nbsp; &nbsp; &nbsp; OffSCREEN_BRIGHT_WAKE_LOCK&nbsp; &nbsp; On&nbsp; &nbsp; &nbsp;Bright&nbsp; &nbsp;OffFULL_WAKE_LOCK&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;On&nbsp; &nbsp; &nbsp;Bright&nbsp; &nbsp;Bright

繁星coding

对我来说,这些方法不起作用。所以我使用其他场景(不是微不足道)来关闭我的屏幕。Android有2个标志,负责清醒:显示 - >屏幕TimeOut应用程序 - >开发 - > 充电时保持清醒复选框。我使用了以下流程:首先保存您之前的配置,例如屏幕超时为1分钟,并 在充电时保持清醒状态。之后,我取消选中充电时保持清醒并将屏幕超时设置为最短时间。我注册广播接收器服务从屏幕关闭的Android获取事件。当我关闭屏幕上的事件时,我将先前的配置设置为默认值:屏幕超时为1分钟并且 在检查充电时保持清醒状态。取消注册接收器15秒后 设备睡觉这是代码片段:广播接收器import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;/**&nbsp;* Catch Screen On/Off&nbsp;* */public class BroadcastReceiverScreenListener extends BroadcastReceiver{private BroadCastListenerCallBackItf mBroadCastListenerCallBack = null;public BroadcastReceiverScreenListener(&nbsp; &nbsp; &nbsp; &nbsp; BroadCastListenerCallBackItf broadCastListenerCallBack) {&nbsp; &nbsp; this.mBroadCastListenerCallBack = broadCastListenerCallBack;}@Overridepublic void onReceive(Context arg0, Intent intent) {&nbsp; &nbsp; if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {&nbsp; &nbsp; &nbsp; &nbsp; mBroadCastListenerCallBack.broadCastListenerCallBack__ScreenOff_onResponse();&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;}}用作回调的接口public interface BroadCastListenerCallBackItf {&nbsp; &nbsp; public void broadCastListenerCallBack__ScreenOff_onResponse();}主类2种方法:....AndroidSynchronize mSync = new AndroidSynchronize();....public void turnScreenOff(int wait){&nbsp; &nbsp; IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);&nbsp; &nbsp; filter.addAction(Intent.ACTION_SCREEN_OFF);&nbsp; &nbsp; BroadCastListenerCallBackItf broadCastListenerCallBack = this;&nbsp; &nbsp; BroadcastReceiver mReceiver = new BroadcastReceiverScreenListener(broadCastListenerCallBack);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; m_context.registerReceiver(mReceiver, filter);&nbsp; &nbsp; //set Development --> disable STAY_ON_WHILE_PLUGGED_IN&nbsp; &nbsp; Settings.System.putInt(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m_context.getContentResolver(),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Settings.System.STAY_ON_WHILE_PLUGGED_IN,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; // take current screen off time&nbsp;&nbsp; &nbsp; int defTimeOut = Settings.System.getInt(m_context.getContentResolver(),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Settings.System.SCREEN_OFF_TIMEOUT, 3000);&nbsp; &nbsp; // set 15 sec&nbsp; &nbsp; Settings.System.putInt(m_context.getContentResolver(),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Settings.System.SCREEN_OFF_TIMEOUT, 15000);&nbsp; &nbsp; // wait 200 sec till get response from BroadcastReceiver on Screen Off&nbsp; &nbsp; mSync.doWait(wait*1000);&nbsp; &nbsp; // set previous settings&nbsp; &nbsp; Settings.System.putInt(m_context.getContentResolver(),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Settings.System.SCREEN_OFF_TIMEOUT, defTimeOut);&nbsp; &nbsp; // switch back previous state&nbsp; &nbsp; Settings.System.putInt(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m_context.getContentResolver(),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Settings.System.STAY_ON_WHILE_PLUGGED_IN,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BatteryManager.BATTERY_PLUGGED_USB);&nbsp; &nbsp; m_context.unregisterReceiver(mReceiver);}public void broadCastListenerCallBack__ScreenOff_onResponse() {&nbsp; &nbsp; mSync.doNotify();}....AndroidSynchronize类public class AndroidSynchronize {&nbsp; &nbsp; public void doWait(long l){&nbsp; &nbsp; &nbsp; &nbsp; synchronized(this){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.wait(l);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch(InterruptedException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; public void doNotify() {&nbsp; &nbsp; &nbsp; &nbsp; synchronized(this) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.notify();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; public void doWait() {&nbsp; &nbsp; &nbsp; &nbsp; synchronized(this){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.wait();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch(InterruptedException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}[编辑]您需要注册权限:android.permission.WRITE_SETTINGS
随时随地看视频慕课网APP

相关分类

Android
我要回答