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.
You can obtain an instance of this class by calling Context.getSystemService().
package com.exercise.AndroidWakeLock;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
public class AndroidWakeLock extends Activity {
WakeLock wakeLock;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
PowerManager powerManager =
(PowerManager)getSystemService(Context.POWER_SERVICE);
wakeLock =
powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK,
"Full Wake Lock");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
wakeLock.acquire();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
wakeLock.release();
}
}
Also have to grant permission of "android.permission.WAKE_LOCK" in AndroidManifest.xml.
No comments:
Post a Comment