获取锁屏的可见性

时间:2013-03-12 17:42:44

标签: android lockscreen keyguard

如果可以看到KeyGuard(锁屏),如何从服务中查看?我想支持原始和自定义锁屏。

1 个答案:

答案 0 :(得分: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的检查并检查时间他们之间。