尝试确定电池电量时,“IntentReceiver组件不允许注册以接收意图”

我正在尝试按照http://developer.android.com/training/monitoring-device-state/battery-monitoring.html中的指南从我的应用程序获取电池信息

这是想出来检查电池电量的方法:

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在类中早先被初始化为Context对象。

这是我得到的错误信息

  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) 

我需要知道的是IntentReceiver components are not allowed to register to receive intents的含义IntentReceiver components are not allowed to register to receive intents以及如何超过它。

谢谢

Solutions Collecting From Web of "尝试确定电池电量时,“IntentReceiver组件不允许注册以接收意图”"