What are wakelocks in android and how do we apply them?
What are wakelocks in android ? Is it like switching the power state of a phone? What class in A开发者_StackOverflow社区ndroid API do we require inorder to apply them?
API level 1
http://developer.android.com/reference/android/os/PowerManager.WakeLock.html
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.PowerManager;
public class DoNotDimScreen extends Activity {
private PowerManager.WakeLock wl;
@Override
protected void onCreate(Bundle. savedInstanceState) {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "DoNotDimScreen");
}
@Override
protected void onPause() {
super.onPause();
wl.release();
}
@Override
protected void onResume() {
super.onResume();
wl.acquire();
}
}
You will need to include this permission in your manifest:
android.permission.WAKE_LOCK
Wake Lock is used in some cases where we need to keep the device on for more than normal time, like while creating some night clock apps where the screen has to remain on.
You have to use
import android.os.PowerManager.WakeLock;
精彩评论