开发者

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:

  1. hold a wakelock at first;
  2. then do something. At the end;
  3. 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();
        }});
  1. press button1
  2. lock the screen for a test
  3. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜