Android PowerManager - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Android PowerManager class.

This code example shows how to use the following methods:newWakeLock, getClass
	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){

7
CodeRank
This code example shows how to use the following methods:newWakeLock
   
  synchronized private static PowerManager.WakeLock getLock(Context context) {
    if (lockStatic==null) {
      PowerManager mgr=(PowerManager)context.getSystemService(Context.POWER_SERVICE);
       
      lockStatic=mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                            LOCK_NAME_STATIC);
      lockStatic.setReferenceCounted(true);
    } 
     
    return(lockStatic);
  } 
   
  public WakefulIntentService(String name) {
    super(name);
  } 
   
  @Override 
  final protected void onHandleIntent(Intent intent) {
    try { 
7
CodeRank
This code example shows how to use the following methods:newWakeLock
    static void runIntentInService(Context context, Intent intent) {
        if (mWakeLock == null) {
            // This is called from BroadcastReceiver, there is no init. 
            PowerManager pm = 
                (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, 
                    WAKELOCK_KEY);
        } 
        mWakeLock.acquire();
        
        // Use a naming convention, similar with how permissions and intents are  
        // used. Alternatives are introspection or an ugly use of statics.  
        String receiver = context.getPackageName() + ".C2DMReceiver";
        intent.setClassName(context, receiver);
         
        context.startService(intent);
 
    } 
     
     
7
CodeRank
This code example shows how to use the following methods:newWakeLock
 
  public MmscProcessor(Context context) {
    this.context = context;
    PowerManager powerManager         = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
    this.connectivityManager          = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
    this.wakeLock                     = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MMS Connection");
    this.wakeLock.setReferenceCounted(false);
  } 
 
  protected String getApnInformation() {
    return connectivityManager.getNetworkInfo(TYPE_MOBILE_MMS).getExtraInfo();
  } 
 
  protected boolean isConnected() { 
    NetworkInfo info = connectivityManager.getNetworkInfo(TYPE_MOBILE_MMS);
 
    Log.w("MmsService", "NetworkInfo: " + info);
 
    if ((info == null) || (info.getType() != TYPE_MOBILE_MMS) || !info.isConnected())
      return false; 
6
CodeRank
This code example shows how to use the following methods:newWakeLock
This code example shows how to use the following methods:newWakeLock, setBacklightBrightness
     *  
     * @throws Exception 
     */ 
    @MediumTest 
    public void testNewWakeLock() throws Exception { 
        PowerManager.WakeLock wl = mPm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "FULL_WAKE_LOCK");
        doTestWakeLock(wl);
 
        wl = mPm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "SCREEN_BRIGHT_WAKE_LOCK");
        doTestWakeLock(wl);
 
        wl = mPm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "SCREEN_DIM_WAKE_LOCK");
        doTestWakeLock(wl);
 
        wl = mPm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "PARTIAL_WAKE_LOCK");
        doTestWakeLock(wl);
         
        doTestSetBacklightBrightness(); 
 
        // TODO: Some sort of functional test (maybe not in the unit test here?)  

7
CodeRank
This code example shows how to use the following methods:newWakeLock, getClass
            boolean proximityLockSupported = false;
            try 
            { 
                if ( Compatibility.isCompatible( 17 ) ) 
                { 
                    Method method = mPowerManager.getClass().getDeclaredMethod( "isWakeLockLevelSupported", int.class );
                    proximityLockSupported = (Boolean) method.invoke( mPowerManager, proximityScreenOffWakeLock );
                } 
                else 
                { 
                    Method method = mPowerManager.getClass().getDeclaredMethod( "getSupportedWakeLockFlags" );
                    int supportedWakeLockFlags = (Integer) method.invoke( mPowerManager );
                    proximityLockSupported = ( ( supportedWakeLockFlags & proximityScreenOffWakeLock ) != 0x0 );
                } 
            } 
            catch ( Exception e )
            { 
                Log.d( TAG, e.getMessage(), e );
            } 
 

6
CodeRank
This code example shows how to use the following methods:newWakeLock
        assertFalse(powerManager.isScreenOn());
    } 
 
    @Test 
    public void shouldCreateWakeLock() throws Exception { 
        assertNotNull(powerManager.newWakeLock(0, "TAG"));
    } 
 
    @Test 
    public void shouldAcquireAndReleaseReferenceCountedLock() throws Exception { 
        PowerManager.WakeLock lock = powerManager.newWakeLock(0, "TAG");
        assertFalse(lock.isHeld());
        lock.acquire();
        assertTrue(lock.isHeld());
        lock.acquire();
 
        assertTrue(lock.isHeld());
        lock.release();
 
        assertTrue(lock.isHeld());
6
CodeRank
This code example shows how to use the following methods:newWakeLock
    static void runIntentInService(Context context, Intent intent) {
        if (mWakeLock == null) {
            // This is called from BroadcastReceiver, there is no init. 
            PowerManager pm = 
                (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, 
                    WAKELOCK_KEY);
        } 
        mWakeLock.acquire();
        
        // Use a naming convention, similar with how permissions and intents are  
        // used. Alternatives are introspection or an ugly use of statics.  
        String receiver = context.getPackageName() + ".C2DMReceiver";
        intent.setClassName(context, receiver);
         
        context.startService(intent);
 
    } 
     
     
6
CodeRank
This code example shows how to use the following methods:isScreenOn
        } 
    } 
 
    private boolean isScreenOn() { 
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        return pm.isScreenOn();
    } 
 
    private void playAlarmSound() { 
        Uri url = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM);
 
        int silentModeStreams = Settings.System.getInt(getContentResolver(),
                Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0);
 
        if ((silentModeStreams & (1 << AudioManager.STREAM_ALARM)) != 0) {
            mPlayer.setAudioStreamType(silentModeStreams);
        } else { 
            mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
        } 
        try { 
6
CodeRank

Related Android PowerManager Questions & Answers:

Other Android PowerManager Examples

9:	        if (wakeLock != null) wakeLock.release();
10:	
11:	        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
12:	        wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
13:	                PowerManager.ACQUIRE_CAUSES_WAKEUP |
-
9:	        if (wakeLock != null) wakeLock.release();
10:	
11:	        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
12:	        wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
13:	                PowerManager.ACQUIRE_CAUSES_WAKEUP |

Read More

See Code Examples for Android PowerManager Methods: