编程语言
首页 > 编程语言> > 如何以编程方式在Android中设置和取消锁定屏幕密码

如何以编程方式在Android中设置和取消锁定屏幕密码

作者:互联网

我想以编程方式设置锁屏密码,然后在执行循环时删除.我已经成功添加了设备管理,有人可以帮助我从我的应用程序本身设置和取消锁定屏幕密码.以下是我的设备管理工作代码

public class DevicePolicyDemoActivity extends Activity {
    static final String TAG = "DevicePolicyDemoActivity";
    static final int ACTIVATION_REQUEST = 47; // identifies our request id
    DevicePolicyManager devicePolicyManager;
    ComponentName demoDeviceAdmin;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Initialize Device Policy Manager service and our receiver class
        devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
        demoDeviceAdmin = new ComponentName(this, DemoDeviceAdminReceiver.class);
        Intent intent = new Intent(
                DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
                demoDeviceAdmin);
        intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
                "Your boss told you to do this");
        startActivityForResult(intent, ACTIVATION_REQUEST);
    }
}

解决方法:

对于设置密码,您可以使用以下代码:

devicePolicyManager.setPasswordQuality(
                                   demoDeviceAdmin
                                  ,DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED);
devicePolicyManager.setPasswordMinimumLength(
                                  demoDeviceAdmin,
                                   5);
boolean result = devicePolicyManager.resetPassword("123456",
                        DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY);

标签:android,device-admin,screen-lock
来源: https://codeday.me/bug/20190830/1766404.html