Wakelock in Android with example


          Wakelock are system service feature that your application can use to control the power state of the host device. Wake Locks can be used to keep the CPU running, prevent the screen from dimming, prevent the screen from turning off and prevent the keyboard back-light from turning off.

For instance, you should have noticed in video players, the screen will not turn off until you quit the player. Such applications makes use of this system service Wake Lock.

Wake Locks has 4 different levels and these are mutually exclusive, you can specify only one at a time.

Different Levels Wake Locks

FULL_WAKE_LOCK -  Keep the screen at full brightness, keyboard back-light illuminated, and the CPU running.
SCREEN_BRIGHT_WAKE_LOCK - Keeps the screen at full brightness and the CPU running.
SCREEN_DIM_WAKE_LOCK - Keeps the screen ON but lets it dim and the CPU running.
PARTIAL_WAKE_LOCK -  Keeps the CPU running

Android permission for Waklock
                 Since wake locks has huge impact on battery life your application needs to request WAKE_LOCK permission in order to create them.
 <uses-permission  android:name="android.permission.WAKE_LOCK"  />

Create Wake Locks
                 To create a wake lock call newWakeLock on the Power manger as follows:
PowerManager.WakeLock wakeLock ;
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
 wakeLock = pm.newWakeLock( PowerManager.SCREEN_DIM_WAKE_LOCK, "My wakelook");

Acquire and Release Wake Locks
                  After creating wake lock, acquire it by calling acquire() method. We can optionally specify timeout to set the maximum time wake lock should be held for. When you wanted the wake lock to end, call release() method. After which the system will manage the power state.
In the following example I have acquired SCREEN_DIM_WAKE_LOCK, switching it ON will acquire the wake lock and OFF will release the wake lock.





MainActivity.java

import android.os.Bundle;
import android.os.PowerManager;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

  PowerManager.WakeLock wakeLock;

  @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
 }

  public void acquirewakeLock(View v) {

   PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
  wakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK,
    "My wakelook");
  wakeLock.acquire();
  Toast acquire = Toast.makeText(getApplicationContext(), "Wake Lock ON",
    Toast.LENGTH_SHORT);
  acquire.show();
 }

  public void releaseWakelock(View v) {
  wakeLock.release();
  Toast release = Toast.makeText(getApplicationContext(),
    "Wake Lock OFF", Toast.LENGTH_SHORT);
  release.show();
 }

  @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }

}



Source Code of this Application
WakeLock_Android.zip




Reactions:

2 comments :