Seems like some of the DeviceAdmin functionality has been broken in Lollipop, at least on my Nexus 5.
This works perfectly on KitKat, JellyBean and Ice Cream Sandwich
@Override
public void onPasswordFailed(Context context, Intent intent) {
DevicePolicyManager policyManager = (DevicePolicyManager)context.getSystemService(Context.DEVICE_POLICY_SERVICE);
if(policyManager != null){
int attempts = policyManager.getCurrentFailedPasswordAttempts();
Log.v("TAG", "Attempts = " + attempts);
}
}
But on my Nexus 5 with Android 5.0 Lollipop it produces the following error:
11-24 16:11:50.117 27053-27053/com.example.myapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.myapp, PID: 27053
java.lang.RuntimeException: Unable to start receiver com.example.myapp.receivers.AdminReceiver: java.lang.NullPointerException: Attempt to read from field 'int android.content.pm.UserInfo.id' on a null object reference
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2586)
at android.app.ActivityThread.access$1700(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to read from field 'int android.content.pm.UserInfo.id' on a null object reference
at android.os.Parcel.readException(Parcel.java:1546)
at android.os.Parcel.readException(Parcel.java:1493)
at android.app.admin.IDevicePolicyManager$Stub$Proxy.getCurrentFailedPasswordAttempts(IDevicePolicyManager.java:2476)
at android.app.admin.DevicePolicyManager.getCurrentFailedPasswordAttempts(DevicePolicyManager.java:1261)
at com.example.myapp.receivers.AdminReceiver.onPasswordFailed(AdminReceiver.java:48)
at android.app.admin.DeviceAdminReceiver.onReceive(DeviceAdminReceiver.java:417)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2579)
at android.app.ActivityThread.access$1700(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
It's been reported as an issue in the L-preview as well.
My question, what is causing this?
I have got same error then I realised that it may not work for the version 5.0 and above versions.So what I did is I am checking number of wrong attempts programmatically.Like If the someone wants to unlock pattern or pin if he enters wrong then receiver will catch and calls the passwordFailed() method.So I created on SharedPreference object and I am storing the number of passwordFailed() method is called. Based on that I am getting number of wrong patterns.I think this is correct way or not I don't know.