check whether lock was enabled or not

后端 未结 9 1309
情书的邮戳
情书的邮戳 2020-11-30 23:52

I have to check whether system lock was enabled or not in settings.

I used below line code

boolean b = android.provider.Settings.System.getInt(
              


        
相关标签:
9条回答
  • 2020-12-01 00:03

    Be careful, this method seems to be outdated too! Thank you Dantalian for the hint!

    LockPatternUtils is a private class. But you can read the lock mode with some reflection: (works with Nexus5, Android 4.4.4)

    private boolean isDeviceSecured()
    {
        String LOCKSCREEN_UTILS = "com.android.internal.widget.LockPatternUtils";
        try
        { 
            Class<?> lockUtilsClass = Class.forName(LOCKSCREEN_UTILS);
            // "this" is a Context, in my case an Activity
            Object lockUtils = lockUtilsClass.getConstructor(Context.class).newInstance(this);
    
            Method method = lockUtilsClass.getMethod("getActivePasswordQuality");
    
            int lockProtectionLevel = (Integer)method.invoke(lockUtils); // Thank you esme_louise for the cast hint
    
            if(lockProtectionLevel >= DevicePolicyManager.PASSWORD_QUALITY_NUMERIC)
            {
                return true;
            }
        }
        catch (Exception e)
        {
            Log.e("reflectInternalUtils", "ex:"+e);
        }
    
        return false;
    }
    
    0 讨论(0)
  • 2020-12-01 00:11

    This can also be achieved using Device Admin Policies http://developer.android.com/guide/topics/admin/device-admin.html

    0 讨论(0)
  • 2020-12-01 00:13

    The above is the hard way. Use KeyguardManager.isKeyguardSecure()

    0 讨论(0)
  • 2020-12-01 00:16
    KeyguardManager km = (KeyguardManager)getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE);
    if(km.isKeyguardSecure())
        Toast.makeText(getApplicationContext(), "locked", Toast.LENGTH_LONG).show();
    else
        Toast.makeText(getApplicationContext(), "Unlocked", Toast.LENGTH_LONG).show();
    
    0 讨论(0)
  • 2020-12-01 00:17

    @Peter Pint and esme_louise

    Thanks, your solution got me going. To find out whether the screen lock is enabled or not I could simplify your method even further. This returns false for swipe or proper lock (PIN, PW, Face unlock, etc.) and returns false for the option 'None'. For the distinction between swipe and one of the proper lock methods I use KeyguardManager.isKeyguardSecure()

    It should work with API level 14+:

    private boolean isLockScreenDisabled(Context context)
    {
        String LOCKSCREEN_UTILS = "com.android.internal.widget.LockPatternUtils";
    
        try
        { 
            Class<?> lockUtilsClass = Class.forName(LOCKSCREEN_UTILS);
            // "this" is a Context, in my case an Activity
            Object lockUtils = lockUtilsClass.getConstructor(Context.class).newInstance(context);
    
            Method method = lockUtilsClass.getMethod("isLockScreenDisabled");
    
            boolean isDisabled = Boolean.valueOf(String.valueOf(method.invoke(lockUtils)));
    
            return isDisabled;
        }
        catch (Exception e)
        {
            Log.e("reflectInternalUtils", "ex:"+e);
        }
    
        return false;
    }
    

    UPDATE: I have adapted the code to android M using the new method isDeviceSecure(). However, this does not allow to differentiate between 'None' and 'Swipe' any more. Additionally, the method already started to fail at 5.x (I think 5.1.1) with a SecurityException. This required an extra hack in the catch block.

    For my purpose of detecting whether the user is absent and USER_PRESTENT will be broadcast when the device is activated/unlocked isDeviceSecure() is good enough and I'm glad to get rid of the brittle reflection stuff for future releases.

    private boolean isLockScreenDisabled(Context context)
    {
        // Starting with android 6.0 calling isLockScreenDisabled fails altogether because the
        // signature has changed. There is a new method isDeviceSecure which, however, does
        // not allow the differentiation between lock screen 'None' and 'Swipe.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
    
            KeyguardManager keyguardMgr = (KeyguardManager) context
                    .getSystemService(Context.KEYGUARD_SERVICE);
    
            // But luckily there is no 'Automatically lock x minutes after sleep' option when 
            // 'Swipe' is set which means that as soon as the screen is off, switching back on 
            // requires a swipe which results in a USER_PRESENT broadcast. 
            return !keyguardMgr.isDeviceSecure();
        }
    
        String LOCKSCREEN_UTILS = "com.android.internal.widget.LockPatternUtils";
    
        try 
        {
            Class<?> lockUtilsClass = Class.forName(LOCKSCREEN_UTILS);
    
            Object lockUtils = lockUtilsClass.getConstructor(Context.class).newInstance(context);
    
            Method method = lockUtilsClass.getMethod("isLockScreenDisabled");
    
            // Starting with android 5.x this fails with InvocationTargetException 
            // (caused by SecurityException - MANAGE_USERS permission is required because
            //  internally some additional logic was added to return false if one can switch between several users)
            // if (Screen Lock is None) { 
            //   ... exception caused by getting all users (if user count)
            // } else {
            //   return false;
            // }
            // -> therefore if no exception is thrown, we know the screen lock setting is
            //    set to Swipe, Pattern, PIN/PW or something else other than 'None'
    
            boolean isDisabled;
            try {
    
                isDisabled = Boolean.valueOf(String.valueOf(method.invoke(lockUtils)));
            }
            catch (InvocationTargetException ex) {
                Log.w(TAG, "Expected exception with screen lock type equals 'None': " + ex);
                isDisabled = true;
            }
            return isDisabled;
        }
        catch (Exception e)
        {
            Log.e(TAG, "Error detecting whether screen lock is disabled: " + e);
    
            e.printStackTrace();
        }
    
        return false;
    }
    

    And this is the method using it: It determines whether the user is absent in a way that the next time the screen is turned on (if no screen lock is set up) or the device is unlocked (including swipe) a USER_PRESENT_ACTION is broadcast.

    public boolean isUserAbsent(Context context) {
    
        KeyguardManager kgMgr = (KeyguardManager) context
                .getSystemService(Context.KEYGUARD_SERVICE);
    
        boolean isDeviceLocked = kgMgr.inKeyguardRestrictedInputMode();
    
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            // there was no 'None' option for screen lock in the olden days
            return isDeviceLocked;
        }
    
        PowerManager powerManager = (PowerManager) context
                .getSystemService(Context.POWER_SERVICE);
    
        if (isLockScreenDisabled(context)) {
    
            // Lock Type 'None' (USER_PRESENT is broadcast when screen comes on)
    
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
                // android 3.0 - 4.1: we have a problem with 'None' because
                // user_present is never broadcast!
                UserLog.log(TAG, context,
                        "No screen lock on android 3.0 - 4.1: User-presence will not be detected! Please switch to 'Swipe'");
            }
    
            return !powerManager.isInteractive();
        } else {
            // Lock Type 'Swipe' or proper lock  (USER_PRESENT is broadcast when device is unlocked)
            return isDeviceLocked;
        }
    }
    
    0 讨论(0)
  • 2020-12-01 00:17

    @Peter Pint

    int lockProtectionLevel = (int)method.invoke(lockUtils);
    

    ...should instead be...

    int lockProtectionLevel = Integer.valueOf(String.valueOf(method.invoke(lockUtils)));
    

    But otherwise, right on! I've upvoted your answer.

    0 讨论(0)
提交回复
热议问题