-2

I want to start service only when device is unlocked. I've tried this code but it didn't work.Can somebody explain me what do I do wrong?And what should I do?

if( myKM.isKeyguardLocked()==false) {
    startService(intent);
    Log.i("Point ","5");
} else {
    Log.i("Point ","6");
    stopService(intent);
}
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
S.Drumble3
  • 59
  • 10
  • Possible duplicate of [android unlock screen intent?](http://stackoverflow.com/questions/2803069/android-unlock-screen-intent) – Strider Feb 22 '16 at 10:51
  • Possible duplicate of [Android - detect phone unlock event, not screen on](http://stackoverflow.com/questions/3446202/android-detect-phone-unlock-event-not-screen-on) – Yash Feb 22 '16 at 10:51

1 Answers1

1

You can have a Broadcast Receiver for ACTION_USER_PRESENT

No you can check in onReceive (Context context, Intent intent) as below -

if(intent.getAction().equals(Intent.ACTION_USER_PRESENT)){
// device unLocked
}
kevz
  • 2,727
  • 14
  • 39