8 Oct 2012

Wake lock in Android.

Its use for control of the power state of the device.
Device battery life will be significantly affected by the use of this API. Do not acquire WakeLocks unless you really need them, use the minimum levels possible, and be sure to release it as soon as you can.

To prevent lock while running the application can be done two ways.
1.PowerManager
2. WindowManager:  Using Power Manager is not a good way. They easily drain battery. So using Window Manager we can avoid the screen locking while application is running. Add the below snippet in your activity to avoid screen lock:  getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
No permissions required while you used Window Manager. 


*Using Power Manager*

import android.app.Activity;
import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.content.Context;
import android.os.PowerManager;
import android.view.WindowManager;
public class AutoLock extends Activity
{
      PowerManager pm;
      PowerManager.WakeLock wl;
      public void WACK_LOCAK_Declare()
      {
      pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
      wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,"MyWakeLock");
      }
      public void WACK_LOCAK_Acquire()
      {
            wl.acquire();
      }
      public void WACK_LOCAK_Release()
      {
            wl.release();
      }
} 

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



I will be happy if you will provide your feedback or follow this blog. Any suggestion and help will be appreciated.
Thank you :)

No comments:

Post a Comment