how to wake lock using powermanager class in android
hi i am trying to wake lock for a particular amount of time but i am not getting the result i am using power manager class but i am not getting any result. i am using this code to wake lock
PowerManager.WakeLock wl;
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
开发者_Python百科 wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
wl.acquire();
wl.release();
}});
}
here i am trying to invoke lock when i click button but its not working. i need to lock when the particular time out using broad cast receiver please suggest me Thanks in advance
you should:
- hold a wakelock at first;
- then do something. At the end;
- release the wakelock.
so you should test it by:
Button b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
wl.acquire();
}});
Button b2=(Button)findViewById(R.id.button2);
b2.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
wl.release();
}});
- press button1
- lock the screen for a test
- press button2
what's more:
- PARTIAL_WAKE_LOCK: Ensures that the CPU is running; the screen and keyboard
- FULL_WAKE_LOCK(I think it's you want): Ensures that the screen and keyboard backlight are on at full brightness.
You acquire and them immediatly release the WL. You have only to acquire WL in OnClick event and to handle a task to release it. I suggest you to use also a mWakeLockAcquired boolean variable to check the WL state.
精彩评论