[Q] Exception when receiving an Intent


we are developing something to watch the battery drain - logging battery % steps - and get sometimes an exception into our Uncaught Exception Handler looks like this:

07-15 12:29:41.815 D/AndroidRuntime(27028): Shutting down VM 07-15 12:29:41.815 W/dalvikvm(27028): threadid=1: thread exiting with uncaught exception (group=0x4001e578) 07-15 12:29:41.815 E/DrainGuardService(27028): Default uncaught exception handler 07-15 12:29:41.815 E/DrainGuardService(27028): Caught throwable java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.BATTERY_CHANGED flg=0x60000000 (has extras) } in for thread Thread[main,5,main]

Thats our Codesnipplet

private IntentFilter batteryLevelFilterpercentchange = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); private BroadcastReceiver batteryLevelReceiver3;
batteryLevelReceiver3 = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { int level = -1; try { int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); if (rawlevel >= 0 && scale > 0) { level = (rawlevel * 100) / scale; } ...

We tried to catch the exception with a try-finally but it looks like we are crashing before.

Funny thing: It happens only 1-2 times a week but we want to fix it. How/where can we catch this exception correctly without ending the app?

I also think that its not our mistake, because the system sends the message with the intent. but sometimes it seems not to be filled in correctly.

Thanks in advance!

Show Accepted Answer

Guest Quick Reply (No URL, BBcode or HTML)

Last post by lromaguera
3 hours ago
Last post by dominic92
3 hours ago
Last post by dante.breitenberg
2 hours ago
Last post by brannon.yundt
34 minutes ago
Last post by yvon
1 hour ago
Last post by trace.jenkins
1 hour ago
Last post by mayer.jaclyn
1 hour ago
Last post by ferry.alysson
1 hour ago