如何在禁用后重新启用键盘锁?

时间:2012-11-24 21:25:19

标签: android locking screen keyguard

我正在制作一个锁屏应用程序,该应用程序使用前台服务,该服务在启动时禁用键盘锁,并在销毁时重新启用它。我可以禁用它,但是当服务停止时它不会重新启用。我正在停止活动中的服务,我知道正在调用onDestroy()因为通知消失了。我在服务中的代码:

@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;
}

}

1 个答案:

答案 0 :(得分:0)

要禁用(解锁)键盘锁,请使用以下代码。这将创建一个新的KeyguardLock,并使用它来禁用键盘锁

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

要重新锁定键盘锁,请使用以下行释放锁定。如果键盘锁被锁定在首位,并且当前没有应用程序正在持有键盘锁

,则会锁定该键盘锁。
keyguardLock.reenableKeyguard();
keyguardLock = null;