title: Getting fatal exception during intent.action.BATTERY_CHANGED
body: <p>I made a simple app that watches the device's battery level.
  I use this code:</p>
  
  <pre><code>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 &gt;= 0 &amp;&amp; scale &gt; 0) {
                  level = (rawlevel * 100) / scale;
              }
              if (DEBUG) {
                  Log.i(TAG, "Charge: " + Integer.toString(level) + "%");
              }
              if (level &gt; 80) {
                  batteryImg.setImageResource(R.drawable.bat100);
              } else if (level &gt; 60) {
                  batteryImg.setImageResource(R.drawable.bat80);
              } else if (level &gt; 40) {
                  batteryImg.setImageResource(R.drawable.bat60);
              } else if (level &gt; 20) {
                  batteryImg.setImageResource(R.drawable.bat40);
              } else if (level &gt; 0) {
                  batteryImg.setImageResource(R.drawable.bat20);
              }
          }
      };
      IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
      activity.registerReceiver(batteryLevelReceiver, batteryLevelFilter);
  }
  </code></pre>
  
  <p>It works properly, but sometimes i get the following exception: (about 10/1 times)</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Anybody see the problem?</p>
  
  <p>E D I T :</p>
  
  <p>I changed the way you told me now i got this:</p>
  
  <pre><code>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)
  </code></pre>