문제

How can I check from a Service if the KeyGuard (Lockscreen) is visible? I want to support the original and custom Lockscreens.

도움이 되었습니까?

해결책

The screen locks only when the device turns the screen off.

You should extend BroadcastReceiver and implement onReceive, like this:

public class YourBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_SCREEN_OFF.equalsIgnoreCase(intent.getAction())) {
            //screen has been switched off!
        }
    }
}

Then you just have to register it and you'll start receiving events when the screen is switched off:

IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
appBroadcastReceiver = new AppBroadcastReceiver(yourActivity);
registerReceiver(appBroadcastReceiver, filter);

There is an edge case where users have their device set to lock n seconds after the screen goes off, you might want to add a check in your broadcast receiver for the ACTION_SCREEN_ON and check the time between them.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top