起動時にキーガードを無効にし、破棄時に再度有効にするフォアグラウンド サービスを使用するロック画面アプリを作成しています。無効にすることはできますが、サービスが停止すると再び有効になりません。アクティビティでサービスを停止していますが、通知が消えたために 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;
}
}