raghumudem
raghumudem

Reputation: 797

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);
        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

Upvotes: 0

Views: 639

Answers (2)

jack guan
jack guan

Reputation: 361

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.

Upvotes: 1

crbin1
crbin1

Reputation: 2229

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.

Upvotes: 0

Related Questions