Code example for PowerManager

Methods: getClass, newWakeLock

0
	private final static int WAIT_FOR_PROXIMITY_NEGATIVE = 1;
	private final static int WAKE_UP_IMMEDIATELY = 0;
 
	public WakeLockManager(Context context){
		mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
		mFullLock = mPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK
											  | PowerManager.ACQUIRE_CAUSES_WAKEUP
											  | PowerManager.ON_AFTER_RELEASE, LOG_TAG);
		mPartialLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, LOG_TAG);
 
		// Check if PROXIMITY_SCREEN_OFF_WAKE_LOCK is implemented, not part of public api. 
		try { 
			Method method = mPowerManager.getClass().getDeclaredMethod("getSupportedWakeLockFlags");
			int supportedWakeLockFlags = (Integer) method.invoke(mPowerManager);
			Field field = PowerManager.class.getDeclaredField("PROXIMITY_SCREEN_OFF_WAKE_LOCK");
			int proximityScreenOffWakeLock = (Integer) field.get(null);
			if ((supportedWakeLockFlags & proximityScreenOffWakeLock) != 0x0) {
				mProximityLock = mPowerManager.newWakeLock(proximityScreenOffWakeLock, LOG_TAG);
			} 
		} catch (Exception e){
			Log.e(LOG_TAG, "Failed to get proximity wake lock.");
		}