如何判断Android手机是否黑屏和锁屏

1. 前言

Android手机在一段时间未操作后,会黑屏,然后点亮屏幕后,会显示锁屏界面。在实现某些功能(例如:手机QQ的手势密码锁定)的时候,需要对黑屏或锁屏进行处理。

2. 解决方案

因为Android手机完全黑屏后,就会自动锁屏的,所以我们完全可以这样认为,黑屏和锁屏是一样的。判断手机是否黑屏或锁屏了,有两种方案,但是如果要判断是否解锁成功了,就只有方案二可以。

2.1 方案一

在 android.app.KeyguardManager 中,有一个方法 inKeyguardRestrictedInputMode 可以用来判断手机是否黑屏。源码如下:

    /**
     * If keyguard screen is showing or in restricted key input mode (i.e. in
     * keyguard password emergency screen). When in such mode, certain keys,
     * such as the Home key and the right soft keys, don't work.
     *
     * @return true if in keyguard restricted input mode.
     *
     * @see android.view.WindowManagerPolicy#inKeyguardRestrictedKeyInputMode
     */
    public boolean inKeyguardRestrictedInputMode() {
        try {
            return mWM.inKeyguardRestrictedInputMode();
        } catch (RemoteException ex) {
            return false;
        }
    }

大概意思就是,如果显示键盘锁屏幕或处于受限键输入模式(即在键盘锁密码紧急屏幕中)。在这种模式下,某些键(如Home键和右软键)不起作用。当处于这种模式下,inKeyguardRestrictedInputMode的返回值为true。经测试,当手机完全黑屏时,返回的结果确实是true。具体如下:

package com.fantasy.base;

import android.app.KeyguardManager;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

public class BaseActivity extends AppCompatActivity {
    public static final String TAB = "SCREEN";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    Log.d(TAB, "是否黑屏:" + isScreenOff(BaseActivity.this));
                }
            }
        }).start();
    }

    /**
     * 屏幕是否黑屏(完全变黑那种,屏幕变暗不算)
     *
     * @param context 上下文
     * @return 屏幕变黑,则返回true;屏幕变亮,则返回false
     */
    public static boolean isScreenOff(Context context) {
        KeyguardManager manager = (KeyguardManager) context.getSystemService(context.KEYGUARD_SERVICE);
        return manager.inKeyguardRestrictedInputMode();
    }

}

2.2 方案二

Android没有提供专门的API来检测当前手机是否锁屏了。但是,在监听机制中,Android有3个广播与锁屏相关。具体情况请看下面的代码:

package com.fantasy.base;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;

public class BaseActivity extends AppCompatActivity {
    public static final String TAB = "SCREEN";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        registerReceiver(mBroadcastReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
        registerReceiver(mBroadcastReceiver, new IntentFilter(Intent.ACTION_SCREEN_ON));
        registerReceiver(mBroadcastReceiver, new IntentFilter(Intent.ACTION_USER_PRESENT));
    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(mBroadcastReceiver);
        super.onDestroy();
    }

    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (!TextUtils.isEmpty(action)) {
                switch (action) {
                    case Intent.ACTION_SCREEN_OFF:
                        Log.d(TAB, "屏幕关闭,变黑");
                        break;
                    case Intent.ACTION_SCREEN_ON:
                        Log.d(TAB, "屏幕开启,变亮");
                        break;
                    case Intent.ACTION_USER_PRESENT:
                        Log.d(TAB, "解锁成功");
                        break;
                    default:
                        break;
                }
            }
        }
    };

}

3. 总结

通过实际应用,个人推荐使用“方案二”,因为“方案一”要做一个定时器不断地去检查,这样比较好资源。

 

 

你可能感兴趣的:(Android)