get_content - intent filter android manifest
"Los componentes de IntentReceiver no pueden registrarse para recibir intentos" al intentar determinar el nivel de la batería (1)
En lugar de:
c.registerReceiver(null, iFilter)
utilizar:
c.getApplicationContext().registerReceiver(null, iFilter)
El Context
que se pasa a onReceive()
está bloqueado para llamar a registerReceiver()
, incluso con un BroadcastReceiver
null
.
Estoy tratando de obtener información de la batería de mi aplicación siguiendo las pautas en http://developer.android.com/training/monitoring-device-state/battery-monitoring.html
Este es el método ideado para verificar el nivel de la batería:
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
se inicializa como objeto de Context
anteriormente en la clase.
Este es el mensaje de error que estoy recibiendo
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)
Lo que necesito saber es que el significado de los IntentReceiver components are not allowed to register to receive intents
de IntentReceiver components are not allowed to register to receive intents
y cómo IntentReceiver components are not allowed to register to receive intents
.
Gracias