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