I am trying to get Battery info from my Application following the guidelines at http://developer.android.com/training/monitoring-device-state/battery-monitoring.html
This is the method is came up with to check the battery level:
public void sendBatteryInfoMessage(){
IntentFilter iFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = c.registerReceiver(null, iFilter);
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean isUsbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean isAcCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
int batteryLevel = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float batteryPct = batteryLevel / (float) scale;
}
c
is initialized as Context
object earlier in the class.
This is the error message I am getting
07-24 18:11:23.445 634-634/wifi.myapp.sudara.lk.sudara_app W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40028890)
07-24 18:11:23.485 634-634/wifi.myapp.sudara.lk.sudara_app E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start receiver wifi.myapp.sudara.lk.sudara_app.SmsActivity: android.content.ReceiverCallNotAllowedException: IntentReceiver components are not allowed to register to receive intents
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2821)
at android.app.ActivityThread.access$3200(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.ReceiverCallNotAllowedException: IntentReceiver components are not allowed to register to receive intents
at android.app.ReceiverRestrictedContext.registerReceiver(ContextImpl.java:138)
at android.app.ReceiverRestrictedContext.registerReceiver(ContextImpl.java:132)
at wifi.myapp.sudara.lk.sudara_app.BatteryInfo.sendBatteryInfoMessage(BatteryInfo.java:25)
at wifi.myapp.sudara.lk.sudara_app.SmsActivity.onReceive(SmsActivity.java:53)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2810)
at android.app.ActivityThread.access$3200(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
What I need to know is the meaning of IntentReceiver components are not allowed to register to receive intents
and how to over come it.
Thank you
Instead of:
c.registerReceiver(null, iFilter)
use:
c.getApplicationContext().registerReceiver(null, iFilter)
The Context
that is passed to onReceive()
is blocked from calling registerReceiver()
, even with a null
BroadcastReceiver
.