我正在制作一个锁屏应用程序,它使用前台服务,在启动时禁用键盘守卫,在销毁时重新启用它。我可以很好地禁用它,但当服务停止时,它不会重新启用。我正在停止活动中的服务,并且我知道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;
}}
发布于 2012-11-25 05:32:32
要禁用(解锁)键盘守卫,请使用以下代码。这将创建一个新的KeyguardLock,并使用它禁用键盘守卫
KeyguardLock keyguardLock = keyguardManager.newKeyguardLock(“Keyguard_Lock_Test”);
keyguardLock.disableKeyguard();要获取键盘守卫,请使用以下代码行释放锁。这将锁定键盘守卫,如果它首先被锁定,并且如果当前没有应用程序持有键盘守卫锁
keyguardLock.reenableKeyguard();
keyguardLock = null;https://stackoverflow.com/questions/13545770
复制相似问题