4

似乎某些 DeviceAdmin 功能在 Lollipop 中已被破坏,至少在我的 Nexus 5 上是这样。

这适用于 KitKat、JellyBean 和 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);
    }
}

但在我的带有 Android 5.0 Lollipop 的 Nexus 5 上,它会产生以下错误:

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-preview 中的一个问题。

我的问题,这是什么原因造成的?

4

1 回答 1

0

我遇到了同样的错误,然后我意识到它可能不适用于 5.0 版及更高版本。所以我所做的是我正在以编程方式检查错误尝试的次数。比如如果有人想解锁图案或输入错误的密码,那么接收器将捕获并调用 passwordFailed() 方法。因此,我在 SharedPreference 对象上创建并存储了调用的 passwordFailed() 方法的数量。基于此,我得到了许多错误的模式。我认为这是正确的方式,我不知道。

于 2015-08-05T06:10:22.007 回答