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 に答える