Android以编程方式获取锁定超时 [英] Android get Lock Timeout Programmatically

查看:211
本文介绍了Android以编程方式获取锁定超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个应用程序,以在手机锁定并关闭屏幕时在锁定屏幕上显示活动. 当用户离开活动时,应显示键盘锁. 检测电话是否被接收者和 ACTION.SCREEN_OFF.锁定的常用方法. 如果用户按下锁定按钮以锁定手机并关闭手机,那么它可以完美地工作. 但是,在ICS之后,屏幕关闭后,手机可能不会立即被锁定.

I am writing an app to show an activity over the lock screen when the phone is locked and screen off. When the user leave the activity, the keyguard should be shown. The common way to detect whether the phone is locked by by receiver and ACTION.SCREEN_OFF. It works perfectly if the user press lock button the lock and screen off the phone. However, after ICS, the phone may not be locked as soon as the phone is screen off.

那么,如何获得锁定事件或如何获得自动锁定的值,如下图所示?

So, how can I get the lock event or how can I get the value of Automatically lock as the picture below?

我知道inKeyguardRestrictedInputMode()是一种检查电话是否已锁定的方法. 但是当手机像接收器一样被锁定时,它无法自动报告.

I know inKeyguardRestrictedInputMode() is a way to check if the phone is locked. but it cannot report automatically when the phone is locked just like receiver.

Android 4.1.2中设置的屏幕截图

推荐答案

我通过使用线程检查设备是否被锁定来解决.

I solved by using thread to check if the device is locked.

服务中的代码部分:

private BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            Log.d("Receiver", "Screen ON");
        } else {
            new Thread(new mRunnable()).start();  
        }
    }
};

private Handler mHandler = new Handler() {
    public void handleMessage(Message msg) {
        switch (msg.what) {
        case 1:
            Intent i = new Intent();
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.setClass(getBaseContext(), activity.class);
            startActivity(i);
            break;
        }
        super.handleMessage(msg);
    }
};

class mRunnable implements Runnable {
    public void run() {
        while (!Thread.currentThread().isInterrupted()) {
            if (isLocked()) {
                mHandler.sendEmptyMessage(1);
                Thread.currentThread().interrupt();
            }
            else {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        }
    }
}

private boolean isLocked() {
    mKeyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
    return mKeyguardManager.inKeyguardRestrictedInputMode();
}

我在这里为寻找答案的其他人列出了它.我只是草拟一个解决方案,我还没有考虑代码和程序的性能.

I listed it here for the others who are finding the answer. I just draft as a solution and I have not consider the performance of the code and program yet.

这篇关于Android以编程方式获取锁定超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆