3

I have read the documnet How can i set up screen lock with a password programmatically?.

I can set a password to lock the screen programmatically using the following code.

DevicePolicyManager devicePolicyManager =(DevicePolicyManager)getApplicationContext().getSystemService(Context.DEVICE_POLICY_SERVICE);
ComponentName demoDeviceAdmin =new ComponentName(this, MyAdmin.class);

devicePolicyManager.setPasswordQuality(demoDeviceAdmin,DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED);
devicePolicyManager.setPasswordMinimumLength(demoDeviceAdmin, 5);

boolean result = devicePolicyManager.resetPassword("123456", DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY);

Toast.makeText(this, "button_lock_password_device..."+result,  Toast.LENGTH_LONG).show()

How can I cancel screen lock with a password programmatically? Could you help me?

And more, how can I set up screen lock with a PIN programmatically? Thanks!

Community
  • 1
  • 1
HelloCW
  • 843
  • 22
  • 125
  • 310

2 Answers2

1

Q: How can I cancel screen lock with a password programmatically?

A:

devicePolicyManager.setPasswordMinimumLength(compName, 0);
boolean result = devicePolicyManager.resetPassword("", DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY);

Q: How can I set up screen lock with a PIN programmatically

A: Your app has to be approved by the user as a Device Administrator. For that you have to add DeviceAdminReceiver, for that you can read more here

P.S. For being admin app, user must have to go to Settings screen and accept you app as Admin.

Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
  • Thanks! To Murtaza Hussain: The code boolean result = devicePolicyManager.resetPassword("", DevicePolicyManager.RESET_PASSWORD_REQUIRE.. works well both android 4.1.2 and android 2.3.6, but It can't work under android 5.0. So I think I should change the lock screen type from Password lock to Swipe lock or None lock programmatically to resolve the question. – HelloCW Feb 03 '15 at 12:56
0

Why you did not create your own lock/unlock code?

You can lock/unlock screen as described here.What remains,is getting password.To do that,use sharedpreferences to save password and when user turn screen on,you only have to get password and compare it with saved password.

Community
  • 1
  • 1
hasanghaforian
  • 13,858
  • 11
  • 76
  • 167
  • Thanks! I have read both http://stackoverflow.com/questions/14352648/how-to-lock-unlock-screen-programmatically and http://stackoverflow.com/questions/12723185/android-screen-lock-unlock-programatically/12723269#12723269. If you have locked a phone with passsword, sometimes you can't cancel it simply, you can read http://stackoverflow.com/questions/28042562/how-to-unlock-android-phone-programmatically-when-i-have-set-password-lock-patte. – HelloCW Feb 03 '15 at 01:24
  • So I hope that I can change the lock screen type from Password lock to Swipe lock programmatically, you can read http://stackoverflow.com/questions/28193714/how-to-change-the-lock-screen-type-from-password-lock-to-swipe-lock-programmatic – HelloCW Feb 03 '15 at 01:26