0

Is there any method to retrive time when device is locked? For example, user locked his device and had it locked for 59 minutes. Then my application checks the time and unlock it.

ramzixp
  • 17,390
  • 3
  • 18
  • 22

2 Answers2

1

I don't know of a direct method. However, you should be able to listen to ACTION_SCREEN_ON and OFF broadcast events and calculate the elapsed time.

muratgu
  • 7,241
  • 3
  • 24
  • 26
0

I dont think that Android will stop you take current time even if device is locked.

You can take current time as below:

Calendar c = Calendar.getInstance(); 
int seconds = c.get(Calendar.SECOND);

And then you can unlock the screen through below code:

 PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
            | PowerManager.ACQUIRE_CAUSES_WAKEUP
            | PowerManager.ON_AFTER_RELEASE, "INFO");
    wl.acquire();

    KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
    KeyguardLock kl = km.newKeyguardLock("name");
    kl.disableKeyguard();

You can also find couple of stack overflow discussions around unlocking like:

Community
  • 1
  • 1
Aman Chhabra
  • 3,824
  • 1
  • 23
  • 39