PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
在上面拿到pm对象之后,这样调用 pm.goToSleep(SystemClock.uptimeMillis());
三. 定时熄灭屏幕
有时屏幕休眠状态会造成断网,
那么这里,还可以介绍一个控制屏幕的方法就是PowerManager.WakeLockPowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "TAG"); wakeLock.acquire();
//释放锁,屏幕熄灭。
wakeLock.release()
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "TAG"); wakeLock.acquire(); 然后 mTimeHandler.postDelayed(new Runnable(){ public void run(){ wakeLock.release(); } }, 10*1000);
那么,这里要注意的是acquire()和release()要成对调用哦!也就是说,你申请了亮屏,过段时间,你要是释放它。
P-Sensor,距离感应器,可以感应手机和人体距离。具体使用用途是在通话过程中打开P-Sensor时靠近脸时屏幕熄灭,第三方apk无法调用,只能系统开发调用
参考http://blog.csdn.net/wds1181977/article/details/18005665
http://bbs.51cto.com/thread-1021568-1.html
Android4.2源码Phone的模块的P-Sensor的控制的类在DisplayPowerController.java
路径:framework/base/services/java/com/android/server/power/DisplayPowerController.java中的:
private PowerManager.WakeLock mProximityWakeLock; mProximityWakeLock = pm.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, LOG_TAG);
打开P-Sensor(正确的说法是获得锁)
if (!mProximityWakeLock.isHeld()) { if (DBG) Log.d(LOG_TAG, "updateProximitySensorMode: acquiring..."); mProximityWakeLock.acquire(); }
释放P-Sensor
if (mProximityWakeLock.isHeld()) { mProximityWakeLock.release(flags) }
这个mProximityWakeLock.acquire();和mProximityWakeLock.release(flags);只是申请这个锁,和释放这个锁,防止其他的调用其中mProximityWakeLock.acquire();会辗转调用到PowerManagerService.java的enableProximityLockLocked()方法,顾名思义,这个方法是打开P-Sensor,是的!这个方法会去判断当前手机有没有P-Sensor,如果有的话,就会去向SensorManager注册一个P-Sensor监听器,那么当P-Sensor检测到手机和人体距离发生改变时,就会调用我们PowerManagerService.java的监听器.同样,当电话挂断时,电话模块会去调用mProximityWakeLock.release(flags), 这样就会取消P-Sensor监听器.
ok.. 那么接下来就是分析PowerManagerService里面这个P-Sensor是怎么工作的。
SensorEventListener mProximityListener = new SensorEventListener() { public void onSensorChanged(SensorEvent event) { long milliseconds = SystemClock.elapsedRealtime(); synchronized (mLocks) { float distance = event.values[0]; //检测到手机和人体的距离 long timeSinceLastEvent = milliseconds - mLastProximityEventTime; //这次检测和上次检测的时间差 mLastProximityEventTime = milliseconds; //更新上一次检测的时间 mHandler.removeCallbacks(mProximityTask); boolean proximityTaskQueued = false; // compare against getMaximumRange to support sensors that only return 0 or 1 boolean active = (distance >= 0.0 && distance < PROXIMITY_THRESHOLD && distance < mProximitySensor.getMaximumRange()); //如果距离小于某一个距离阈值,默认是5.0f,说明手机和脸部距离贴近,应该要熄灭屏幕。 if (mDebugProximitySensor) { Slog.d(TAG, "mProximityListener.onSensorChanged active: " + active); } if (timeSinceLastEvent < PROXIMITY_SENSOR_DELAY) { // enforce delaying atleast PROXIMITY_SENSOR_DELAY before processing mProximityPendingValue = (active ? 1 : 0); mHandler.postDelayed(mProximityTask, PROXIMITY_SENSOR_DELAY - timeSinceLastEvent); proximityTaskQueued = true; } else { // process the value immediately mProximityPendingValue = -1; proximityChangedLocked(active); //熄灭屏幕操作 } // update mProximityPartialLock state boolean held = mProximityPartialLock.isHeld(); if (!held && proximityTaskQueued) { // hold wakelock until mProximityTask runs mProximityPartialLock.acquire(); } else if (held && !proximityTaskQueued) { mProximityPartialLock.release(); } } } public void onAccuracyChanged(Sensor sensor, int accuracy) { // ignore } };
代码:
private static final int PROXIMITY_SENSOR_DELAY = 1000;
proximityChangedLocked(active);
if (!mProximitySensorEnabled) { Slog.d(TAG, "Ignoring proximity change after sensor is disabled"); return; }
goToSleepLocked(SystemClock.uptimeMillis(), WindowManagerPolicy.OFF_BECAUSE_OF_PROX_SENSOR);