在Android上closures屏幕

我正试图在某个动作发生后打开和closures显示器(让我们只是担心现在closures屏幕)。 从我从wake lock所了解到的,这就是我所拥有的:

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

当我读到其他post在stackoverflow和其他地方,他们似乎告诉我,PARTIAL_WAKE_LOCK将closures屏幕。 但是,如果我阅读SDK说,它只会允许屏幕closures。 所以我认为这是不对的。

任何提示将有所帮助! 谢谢,

麦克风

closures屏幕有两种select:

 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会closures屏幕。

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

对我而言,这些方法不起作用。 所以我用其他场景(不是微不足道的)来closures我的屏幕。

Android有两个标志,负责清醒:

  • 显示 – >屏幕超时
  • 应用程序 – >开发 – > 充电时保持清醒checkbox。

我用了下面的stream程:

  1. 首先保存以前的configuration,例如屏幕超时时间为1分钟,检查充电时保持清醒状态

  2. 之后,我不检查保持清醒,同时充电和设置屏幕超时时间最短。

  3. 我注册广播接收器服务,从Android屏幕closures的事件。

  4. 当我在屏幕上closures事件时,我将之前的configuration设置为默认值:屏幕超时为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(); } } } 

用作callback的接口

 public interface BroadCastListenerCallBackItf { public void broadCastListenerCallBack__ScreenOff_onResponse(); } 

主类有两种方法:

 .... 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 

通过这个链接 ,你也可以像这样closures屏幕:

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

1000以毫秒为单位,这意味着1秒,您可以根据需要用任何值replace它。

需要许可:

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

试试 – wakeLock.acquire(1000); //指定时间,它变暗并最终closures。