How to get lock screen wrong passcode entered?

29 Views Asked by At

I am making a security app. I want to notify my users when they entered to lock screen wrong passcode. I have searched on internet and stackoverflow.com but all links are dead. And My example app is not working. The problem is "No active admin owned by uid 10418 for policy #0". But it should works smoothly. It always throw me security error.

Here is the github links that I coded on my project:

https://github.com/commonsguy/cw-omnibus/tree/master/DeviceAdmin/PasswordEnforcer/app

Also, here is my code:

MainActivity:

ComponentName cn=new ComponentName(this, AdminReceiver.class);
DevicePolicyManager mgr=
        (DevicePolicyManager)getSystemService(DEVICE_POLICY_SERVICE);

if (mgr.isAdminActive(cn)) {
    int msgId;

    if (mgr.isActivePasswordSufficient()) {
        msgId=R.string.compliant;
    }
    else {
        msgId= R.string.not_compliant;
    }

    Toast.makeText(this, msgId, Toast.LENGTH_LONG).show();
}
else {
    Intent intent=
            new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
    intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, cn);
    intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
            getString(R.string.device_admin_explanation));
    startActivity(intent);
}

finish();

AdminReceiver class:

public class AdminReceiver extends DeviceAdminReceiver {
    @Override
    public void onEnabled(Context ctxt, Intent intent) {
        ComponentName cn = new ComponentName(ctxt, AdminReceiver.class);
        DevicePolicyManager mgr =
                (DevicePolicyManager) ctxt.getSystemService(Context.DEVICE_POLICY_SERVICE);

        mgr.setPasswordQuality(cn,
                DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC);

        onPasswordChanged(ctxt, intent);
    }

    @Override
    public void onPasswordChanged(Context ctxt, Intent intent) {
        DevicePolicyManager mgr =
                (DevicePolicyManager) ctxt.getSystemService(Context.DEVICE_POLICY_SERVICE);
        int msgId;

        if (mgr.isActivePasswordSufficient()) {
            msgId = R.string.compliant;
        } else {
            msgId = R.string.not_compliant;
        }

        Toast.makeText(ctxt, msgId, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onPasswordFailed(Context ctxt, Intent intent) {
        Toast.makeText(ctxt, R.string.password_failed, Toast.LENGTH_LONG)
                .show();
    }

    @Override
    public void onPasswordSucceeded(Context ctxt, Intent intent) {
        Toast.makeText(ctxt, R.string.password_success, Toast.LENGTH_LONG)
                .show();

    }
}

Xml Folder:

<device-admin xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-policies>
        <limit-password/>

        <watch-login/>
    </uses-policies>

</device-admin>

How can I fix it?

0

There are 0 best solutions below