Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to SET and UNSET lock screen password in Android programatically

I want to set lock screen password programatically and then remove when the loop is executed. I have added Device Administration successfully, can someone help me to SET and UNSET lock screen password from my application itself. Below is my working code for Device Administration

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);
    }
}
like image 980
user45678 Avatar asked Oct 18 '25 10:10

user45678


1 Answers

For Setting Password you can use the code below:

devicePolicyManager.setPasswordQuality(
                                   demoDeviceAdmin
                                  ,DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED);
devicePolicyManager.setPasswordMinimumLength(
                                  demoDeviceAdmin,
                                   5);
boolean result = devicePolicyManager.resetPassword("123456",
                        DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY);
like image 106
Arslan Sohail Avatar answered Oct 19 '25 22:10

Arslan Sohail



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!