1

我制作了一个简单的应用程序来监视设备的电池电量。我使用这段代码:

private void setProperImageByBatteryLevel() {

    BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            context.unregisterReceiver(this);
            int rawlevel = intent.getIntExtra("level", -1);
            int scale = intent.getIntExtra("scale", -1);
            int level = -1;
            if (rawlevel >= 0 && scale > 0) {
                level = (rawlevel * 100) / scale;
            }
            if (DEBUG) {
                Log.i(TAG, "Charge: " + Integer.toString(level) + "%");
            }
            if (level > 80) {
                batteryImg.setImageResource(R.drawable.bat100);
            } else if (level > 60) {
                batteryImg.setImageResource(R.drawable.bat80);
            } else if (level > 40) {
                batteryImg.setImageResource(R.drawable.bat60);
            } else if (level > 20) {
                batteryImg.setImageResource(R.drawable.bat40);
            } else if (level > 0) {
                batteryImg.setImageResource(R.drawable.bat20);
            }
        }
    };
    IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    activity.registerReceiver(batteryLevelReceiver, batteryLevelFilter);
}

它工作正常,但有时我会遇到以下异常:(大约 10/1 次)

01-21 13:16:56.617: E/AndroidRuntime(7810): FATAL EXCEPTION: main
01-21 13:16:56.617: E/AndroidRuntime(7810): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.BATTERY_CHANGED flg=0x60000000 (has extras) } in hu.trendency.activitimap.widgets.Battery$2@4053e3b8
01-21 13:16:56.617: E/AndroidRuntime(7810):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:722)
01-21 13:16:56.617: E/AndroidRuntime(7810):     at android.os.Handler.handleCallback(Handler.java:587)
01-21 13:16:56.617: E/AndroidRuntime(7810):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-21 13:16:56.617: E/AndroidRuntime(7810):     at android.os.Looper.loop(Looper.java:123)
01-21 13:16:56.617: E/AndroidRuntime(7810):     at android.app.ActivityThread.main(ActivityThread.java:3687)
01-21 13:16:56.617: E/AndroidRuntime(7810):     at java.lang.reflect.Method.invokeNative(Native Method)
01-21 13:16:56.617: E/AndroidRuntime(7810):     at java.lang.reflect.Method.invoke(Method.java:507)
01-21 13:16:56.617: E/AndroidRuntime(7810):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
01-21 13:16:56.617: E/AndroidRuntime(7810):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-21 13:16:56.617: E/AndroidRuntime(7810):     at dalvik.system.NativeStart.main(Native Method)
01-21 13:16:56.617: E/AndroidRuntime(7810): Caused by: java.lang.IllegalArgumentException: Receiver not registered: hu.trendency.activitimap.widgets.Battery$2@4053e3b8
01-21 13:16:56.617: E/AndroidRuntime(7810):     at android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:610)
01-21 13:16:56.617: E/AndroidRuntime(7810):     at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:919)
01-21 13:16:56.617: E/AndroidRuntime(7810):     at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:331)
01-21 13:16:56.617: E/AndroidRuntime(7810):     at hu.trendency.activitimap.widgets.Battery$2.onReceive(Battery.java:92)
01-21 13:16:56.617: E/AndroidRuntime(7810):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:709)
01-21 13:16:56.617: E/AndroidRuntime(7810):     ... 9 more

有人看到问题了吗?

编辑 :

我改变了你告诉我的方式,现在我得到了这个:

01-23 14:43:40.205: E/AndroidRuntime(27064): FATAL EXCEPTION: main
01-23 14:43:40.205: E/AndroidRuntime(27064): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.BATTERY_CHANGED flg=0x60000000 (has extras) } in hu.trendency.activitimap.widgets.Battery$2@40534080
01-23 14:43:40.205: E/AndroidRuntime(27064):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:722)
01-23 14:43:40.205: E/AndroidRuntime(27064):    at android.os.Handler.handleCallback(Handler.java:587)
01-23 14:43:40.205: E/AndroidRuntime(27064):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-23 14:43:40.205: E/AndroidRuntime(27064):    at android.os.Looper.loop(Looper.java:123)
01-23 14:43:40.205: E/AndroidRuntime(27064):    at android.app.ActivityThread.main(ActivityThread.java:3687)
01-23 14:43:40.205: E/AndroidRuntime(27064):    at java.lang.reflect.Method.invokeNative(Native Method)
01-23 14:43:40.205: E/AndroidRuntime(27064):    at java.lang.reflect.Method.invoke(Method.java:507)
01-23 14:43:40.205: E/AndroidRuntime(27064):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
01-23 14:43:40.205: E/AndroidRuntime(27064):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-23 14:43:40.205: E/AndroidRuntime(27064):    at dalvik.system.NativeStart.main(Native Method)
01-23 14:43:40.205: E/AndroidRuntime(27064): Caused by: java.lang.IllegalArgumentException: Receiver not registered: hu.trendency.activitimap.widgets.Battery$2@40534080
01-23 14:43:40.205: E/AndroidRuntime(27064):    at android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:610)
01-23 14:43:40.205: E/AndroidRuntime(27064):    at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:919)
01-23 14:43:40.205: E/AndroidRuntime(27064):    at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:331)
01-23 14:43:40.205: E/AndroidRuntime(27064):    at hu.trendency.activitimap.widgets.Battery$2.onReceive(Battery.java:93)
01-23 14:43:40.205: E/AndroidRuntime(27064):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:709)
4

1 回答 1

3

改变

context.unregisterReceiver(this);

activity.unregisterReceiver(this);

因为您进入的上下文onReceive()可能与您最初用于注册接收者的上下文相同,也可能不同。

您还可以unregisterReceiver()使用 try/catch 块和 catch 和 ignore围绕调用IllegalArgumentException

于 2013-01-22T12:15:20.503 回答