4

我正在制作一个使用前台服务的锁屏应用程序,该服务在启动时禁用键盘保护并在销毁时重新启用它。我可以很好地禁用它,但是当服务停止时它不会重新启用。我正在停止活动中的服务,并且我知道 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;
}

}

4

1 回答 1

0

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

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

要重新启用键盘保护,请使用以下行来释放锁定。如果键盘锁首先被锁定,并且当前没有应用程序持有键盘锁,这将锁定键盘锁

keyguardLock.reenableKeyguard();
keyguardLock = null;
于 2012-11-24T21:32:32.027 回答