4

I'm making a lock screen app that uses a foreground service which disables the keyguard when it is started and reenables it when it is destroyed. I can disable it fine, but it doesn't reenable when the service is stopped. I'm stopping the service in an activity, and I know the onDestroy() is being called because the notification goes away. My code in the service:

@Override
public void onDestroy() {
    isRunning = false;
    KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Service.KEYGUARD_SERVICE);
    KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
    lock.reenableKeyguard();
    stopForeground(true);  
    super.onDestroy(); 
}



@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Service.KEYGUARD_SERVICE);
    KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
    lock.disableKeyguard();

    Notification notification = new Notification(R.drawable.ic_launcher, getText(R.string.ticker_text),
            System.currentTimeMillis());
    Intent notificationIntent = new Intent(this, LockService.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    notification.setLatestEventInfo(this, getText(R.string.notification_title),
            getText(R.string.notification_message), pendingIntent);
    startForeground(1, notification);

    return Service.START_STICKY;
}

}

Praful Bhatnagar
  • 7,425
  • 2
  • 36
  • 44
CoffeeCrisp
  • 253
  • 3
  • 6
  • 19

1 Answers1

0

To disable (unlock) the keyguard, use the following code. This creates a new KeyguardLock and uses it to disable the keyguard

KeyguardLock keyguardLock = keyguardManager.newKeyguardLock(“Keyguard_Lock_Test”);
keyguardLock.disableKeyguard();

To reanable the keyguard, use the following lines to release the lock. This will lock the keyguard if it was locked in the first place, and if no application is currently holding a Keyguard Lock

keyguardLock.reenableKeyguard();
keyguardLock = null;
Artyom Kiriliyk
  • 2,513
  • 1
  • 17
  • 21
  • I'm getting an error on the first line of code for the string. – CoffeeCrisp Nov 24 '12 at 21:49
  • Nevermind, fixed that. But how do I put the disable and reenable in two different methods (onStartCommand and onDestroy?) The app is crashing if I declare the keyguardLock as a global variable. – CoffeeCrisp Nov 24 '12 at 21:52
  • You can make global variable `KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);` but you may duplicate initializing of keyguardLock. What is your error? – Artyom Kiriliyk Nov 24 '12 at 21:59
  • It compiles, but it fails to instantiate the service when I have that as a global variable. It's a NullPointerException. – CoffeeCrisp Nov 24 '12 at 22:01
  • Is there a way to reenable the keyguard, sucht that it does not immedieately pops up? I only want the screenlock to be shown when the users presses the power button. After disabling the keyguard there is no more keyguard available at all. When doing reenableKeykuard the screen gets locked. Ideas? – softwaresupply Jan 06 '15 at 15:09