似乎Lollipop中的某些DeviceAdmin功能已被破坏,至少在我的Nexus 5上。
这非常适合KitKat,JellyBean和冰淇淋三明治
@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);
}
}
但是在我的 Nexus 5 和 Android 5.0 棒棒糖上,它会产生以下错误:
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)
在L预览中也将其报告为问题。
我的问题,是什么原因造成的?
我遇到了同样的错误,然后我意识到它可能不适用于 5.0 及以上版本。所以我所做的是以编程方式检查错误尝试的次数。就像如果有人想要解锁图案或引脚,如果他输入错误,那么接收器将捕获并调用 passwordFailed() 方法。所以我在共享首选项对象上创建了,我正在存储调用的密码失败()方法的数量。基于此,我得到了许多错误的模式。我认为这是正确的方式,我不知道。