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
の意味とそれを克服する方法です。
ありがとうございました
の代わりに:
_c.registerReceiver(null, iFilter)
_
使用する:
_c.getApplicationContext().registerReceiver(null, iFilter)
_
onReceive()
に渡されたContext
は、null
BroadcastReceiver
を使用しても、registerReceiver()
の呼び出しをブロックされます。