0

I successfully got administrator privileges everything works fine. Only problem is that DeviceAdminReceiver is never being called I am trying to check for number of failed passwords.

public class MyAdminReceiver extends DeviceAdminReceiver {

void showToast(Context context, CharSequence msg) {
    Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}

@Override
public void onPasswordFailed(Context context, Intent intent) {
    showToast(context, "Sample Device Admin: pw failed");
    Log.d("Hello", "onPasswordFailed");
    DevicePolicyManager mgr = (DevicePolicyManager)context.getSystemService(Context.DEVICE_POLICY_SERVICE);
    int no = mgr.getCurrentFailedPasswordAttempts();


    if (no >= 3) {
        showToast(context, "3 failure");
        mgr.resetPassword("111111", DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY);
        //mgr.lockNow();
    }
}


@Override
public void onEnabled(Context context, Intent intent) {
    showToast(context, "Sample Device Admin: enabled");
}

@Override
public CharSequence onDisableRequested(Context context, Intent intent) {
    return "This is an optional message to warn the user about disabling.";
}

@Override
public void onDisabled(Context context, Intent intent) {
    showToast(context, "Sample Device Admin: disabled");
}

@Override
public void onPasswordChanged(Context context, Intent intent) {
    showToast(context, "Sample Device Admin: pw changed");
}



@Override
public void onPasswordSucceeded(Context context, Intent intent) {
    showToast(context, "Sample Device Admin: pw succeeded");
}

}

I also declared it in my manifest

 <receiver
        android:name=".MyAdminReceiver"
        android:permission="android.permission.BIND_DEVICE_ADMIN">
        <meta-data android:name="android.app.device_admin"
            android:resource="@xml/device_admin" />
        <intent-filter>
            <action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
        </intent-filter>
    </receiver>

and device_admin

<?xml version="1.0" encoding="utf-8"?>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-policies>
        <limit-password />
        <watch-login />
        <reset-password />
        <force-lock />
        <wipe-data />
        <expire-password />
        <encrypted-storage />
        <disable-camera />
    </uses-policies>
</device-admin>

Can anyone give me an advice what is the cause of this ? Thank you.

  • What is the content of `res/xml/device_admin`? – CommonsWare Mar 19 '17 at 21:23
  • Well, that looks OK (other than you asking for a *lot* of admin capabilities...). The last time I tried [this book sample](https://github.com/commonsguy/cw-omnibus/tree/master/DeviceAdmin/PasswordEnforcer), it worked fine. Try it, or see if there is anything notably different between your implementation and mind. – CommonsWare Mar 19 '17 at 22:17
  • Possible duplicate of https://stackoverflow.com/questions/8067328/having-trouble-receiving-action-password-succeeded-and-action-password-failed-in/44643562#44643562. Please see my answer there. Problem is most likely due to you using incorrect passwords/pins/patterns with length < 4. – Matt R Jun 20 '17 at 04:03

1 Answers1

1

Maybe change

<intent-filter>
    <action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
</intent-filter>

to

<intent-filter>
    <action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
    <action android:name="android.app.action.ACTION_PASSWORD_FAILED"/>
    <action android:name="android.app.action.ACTION_PASSWORD_SUCCEEDED"/>
</intent-filter> 
this.user
  • 26
  • 2