1

我如何从服务中检查 KeyGuard(锁屏)是否可见?我想支持原始和自定义锁屏。

4

1 回答 1

4

仅当设备关闭屏幕时屏幕才会锁定。

您应该扩展BroadcastReceiver和实现onReceive,如下所示:

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!
        }
    }
}

然后你只需要注册它,当屏幕关闭时你就会开始接收事件:

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

有一种极端情况,用户将他们的设备设置为n在屏幕关闭后锁定几秒钟,您可能需要在广播接收器中添加一个检查ACTION_SCREEN_ON并检查它们之间的时间。

于 2013-03-12T17:46:14.220 回答