mehmetk
mehmetk

Reputation: 19

Android - When my phone is locked, my app is not working

I have a application. When i press volume down button, counter is increasing. but i want to do when my device is lock.

When i pressed power button, counter is not working.

if (event.getAction() == KeyEvent.ACTION_DOWN)
    {
        switch (event.getKeyCode()) 
        {
            case KeyEvent.KEYCODE_VOLUME_DOWN:

            counter=counter+1;
        }
    }

Upvotes: 1

Views: 599

Answers (1)

shallOvercome
shallOvercome

Reputation: 396

MyAs mentioned in the comments of your question, you can use service.

Alternatively, I guess it can be done in Activity with the use of wakelock too. Acquire a partial wakelock in your activity. This way, the CPU wont go to sleep and your activity keeps running.

For acquiring wakelock:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Wakelock");
wl.acquire();

For releasing wakelock:

  wl.release();

You can acquire wakelock when your activity is created and release the wakelock when the activity is destroyed.

Upvotes: 1

Related Questions