以编程方式更改PIN

3
在Android设备中,我们可以有三种类型的锁屏方式:图案锁、PIN码锁和密码锁。
在我的应用程序中,我想通过编程方式更改/重置PIN码。
我将从用户那里接受4位数字,并将其设置为该设备的新PIN码。
是否可以重置此PIN码或者这是违反Android安全策略的行为?
谢谢您。
2个回答

2
您可以使用设备管理 API来实现。Android 提供了自定义密码逻辑的功能(甚至更多)。请阅读如何实施密码策略。 注意:您可以在<sdk>/samples/android-<version>/获取示例代码。<version>数字对应平台的 API 级别。

你好,能否告诉我们现在是否有任何方法可以更改密码,因为Android Nougat的DeviceAdmin API已经更改了。 - Piyush
@piyush,密码相关的API没有任何更改。它们在Nougat中也应该可以正常工作。 - Pankaj Kumar
1
它已更改为适用于Android 7,请查看:https://developer.android.com/reference/android/app/admin/DevicePolicyManager.html#resetPassword(java.lang.String, int) - Piyush
@piyush 是的,你说得对。感谢您的纠正。根据文档,只有在设备没有设置密码的情况下,此API才能正常工作。但是在设备所有者或配置文件所有者的情况下,它将按预期工作。我会进一步了解并详细更新答案。 - Pankaj Kumar

0

使用反射是最好的方法。使用反射,您可以将设备密码设置为无、滑动、PIN、密码。 以下是帮助您的代码。确保您拥有管理员权限。此代码将更改您的设备密码为滑动。在resetpassword方法中,对于pin,请给出一些值而不是空字符串""。对于无操作设备策略管理器,确保您的应用程序是系统应用程序,即系统签名。

try
                            {  
                                try{
                                    Class lockPatternUtilsCls = Class.forName("com.android.internal.widget.LockPatternUtils");
                                    Constructor lockPatternUtilsConstructor = 
                                        lockPatternUtilsCls.getConstructor(new Class[]{Context.class});
                                    lockPatternUtilsConstructor.setAccessible(true);
                                    Object lockPatternUtils = lockPatternUtilsConstructor.newInstance(ChangeDeviceLockMode.this);
                                    Method clearLockMethod = lockPatternUtils.getClass().getMethod("clearLock", boolean.class);
                                    clearLockMethod.setAccessible(true);
                                    clearLockMethod.invoke(lockPatternUtils, true);
                                    Method setLockScreenDisabledMethod = lockPatternUtils.getClass().getMethod("setLockScreenDisabled", boolean.class);
                                    setLockScreenDisabledMethod.setAccessible(true);
                                    setLockScreenDisabledMethod.invoke(lockPatternUtils, false);     
                            }catch(Exception e){
                                System.err.println("An InvocationTargetException was caught!");
                                                    Throwable cause = e.getCause();
                            }
                                devicePolicyManager.setPasswordQuality(demoDeviceAdmin, DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED);
                                devicePolicyManager.setPasswordMinimumLength(demoDeviceAdmin, 0);
                                boolean result = devicePolicyManager.resetPassword("", DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY);
                            }
                            catch(Exception ex)
                            {
                                ex.printStackTrace();
                            }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接