电池改变广播接收器崩溃的应用程序在某些手机上 [英] Battery changed broadcast receiver crashing app on some phones
问题描述
我启动我的应用程序,只要手机获得插上电源。 这是我的清单
<接收机器人:名称=。PowerConnectionReceiver>
<意向滤光器>
<作用机器人:名称=android.intent.action.ACTION_POWER_CONNECTED/>
<作用机器人:名称=android.intent.action.ACTION_POWER_DISCONNECTED/>
&所述; /意图滤光器>
< /接收器>
这是我的广播接收器
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
Log.w(TAG,刚收到连接广播);
IntentFilter的IFilter的=新的IntentFilter(Intent.ACTION_BATTERY_CHANGED);
意向电池状态= context.registerReceiver(空,IFilter的); //此行是导致飞机失事的原因
INT状态= batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS,0);
现在,造成我的朋友们的电话了崩溃(对我的一切工作完美地)线是这个
意向电池状态= context.registerReceiver(空,IFilter的);
是否有人可以告诉我是什么原因,如何解决? 我的手机是Galaxy Nexus的最新的Android JB,他的手机是LG的Optimus One姜饼
下面是logcat的
1月二号至27日:25:19.399:D / AndroidRuntime(5326):关闭虚拟机
1月2日至27号:25:19.399:W / dalvikvm(5326):主题ID = 1:螺纹退出与未捕获的异常(组= 0x40018560)
1月2日至27号:25:19.419:E / AndroidRuntime(5326):致命异常:主要
1月2日至27号:25:19.419:E / AndroidRuntime(5326):java.lang.RuntimeException的:无法启动接收器com.doublep.wakey.PowerConnectionReceiver:android.content.ReceiverCallNotAllowedException:IntentReceiver组件不允许注册接收意图
1月2日至27号:25:19.419:E / AndroidRuntime(5326):在android.app.ActivityThread.handleReceiver(ActivityThread.java:1926)
1月2日至27号:25:19.419:E / AndroidRuntime(5326):在android.app.ActivityThread.access $ 2400(ActivityThread.java:123)
1月2日至27号:25:19.419:E / AndroidRuntime(5326):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:989)
1月2日至27号:25:19.419:E / AndroidRuntime(5326):在android.os.Handler.dispatchMessage(Handler.java:99)
1月2日至27号:25:19.419:E / AndroidRuntime(5326):在android.os.Looper.loop(Looper.java:130)
1月2日至27号:25:19.419:E / AndroidRuntime(5326):在android.app.ActivityThread.main(ActivityThread.java:3835)
1月2日至27号:25:19.419:E / AndroidRuntime(5326):在java.lang.reflect.Method.invokeNative(本机方法)
1月2日至27号:25:19.419:E / AndroidRuntime(5326):在java.lang.reflect.Method.invoke(Method.java:507)
1月2日至27号:25:19.419:E / AndroidRuntime(5326):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:864)
1月2日至27号:25:19.419:E / AndroidRuntime(5326):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
1月2日至27号:25:19.419:E / AndroidRuntime(5326):在dalvik.system.NativeStart.main(本机方法)
1月2日至27号:25:19.419:E / AndroidRuntime(5326):由:android.content.ReceiverCallNotAllowedException:IntentReceiver组件不允许注册接收意图
1月2日至27号:25:19.419:E / AndroidRuntime(5326):在android.app.ReceiverRestrictedContext.registerReceiver(ContextImpl.java:151)
1月2日至27号:25:19.419:E / AndroidRuntime(5326):在android.app.ReceiverRestrictedContext.registerReceiver(ContextImpl.java:145)
1月2日至27号:25:19.419:E / AndroidRuntime(5326):在com.doublep.wakey.PowerConnectionReceiver.onReceive(PowerConnectionReceiver.java:46)
1月2日至27号:25:19.419:E / AndroidRuntime(5326):在android.app.ActivityThread.handleReceiver(ActivityThread.java:1915)
1月2日至27号:25:19.419:E / AndroidRuntime(5326):10 ...更多
您已经看到了的 ReceiverCallNotAllowedException
。
这异常是从
registerReceiver(BroadcastReceiver的抛出, IntentFilter的)
和bindService(意向,ServiceConnection,INT)
时, 这些方法正在从一个部件的BroadcastReceiver使用。在 这种情况下,组件将不再被激活时从返回 接收到意图,因此它是无效使用异步的API。
这意味着,你不能注册的BroadcastReceiver
的在的一个的BroadcastReceiver
编辑:
例如工作的一个BroadcastReceiver,它监听到 ACTION_BATTERY_CHANGED
意图:
私人诠释mBatteryLevel;
私人的IntentFilter mBatteryLevelFilter;
BroadcastReceiver的mBatteryReceiver =新的BroadcastReceiver(){
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
mBatteryLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL,-1);
Toast.makeText(背景下,当前电池电量:+ mBatteryLevel,Toast.LENGTH_LONG).show();
}
};
私人无效registerMyReceiver(){
mBatteryLevelFilter =新的IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(mBatteryReceiver,mBatteryLevelFilter);
}
现在,你只需要调用 registerMyReceiver()
。
I activate my application whenever the phone get plugged in a power source. This is my manifest
<receiver android:name=".PowerConnectionReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</receiver>
This is my broadcast receiver
@Override
public void onReceive(Context context, Intent intent) {
Log.w("TAG", "Just received connection broadcast");
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter); //this line is causing the crash
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, 0);
Now, the line causing the crashing on my friends phone (on mine everything is working perfectly) is this one
Intent batteryStatus = context.registerReceiver(null, ifilter);
Can someone please tell me what is the reason and how to fix it? My phone is a galaxy nexus with the latest android JB, his phone is a LG optimus one with gingerbread
Here is the logcat
02-27 01:25:19.399: D/AndroidRuntime(5326): Shutting down VM
02-27 01:25:19.399: W/dalvikvm(5326): threadid=1: thread exiting with uncaught exception (group=0x40018560)
02-27 01:25:19.419: E/AndroidRuntime(5326): FATAL EXCEPTION: main
02-27 01:25:19.419: E/AndroidRuntime(5326): java.lang.RuntimeException: Unable to start receiver com.doublep.wakey.PowerConnectionReceiver: android.content.ReceiverCallNotAllowedException: IntentReceiver components are not allowed to register to receive intents
02-27 01:25:19.419: E/AndroidRuntime(5326): at android.app.ActivityThread.handleReceiver(ActivityThread.java:1926)
02-27 01:25:19.419: E/AndroidRuntime(5326): at android.app.ActivityThread.access$2400(ActivityThread.java:123)
02-27 01:25:19.419: E/AndroidRuntime(5326): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:989)
02-27 01:25:19.419: E/AndroidRuntime(5326): at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 01:25:19.419: E/AndroidRuntime(5326): at android.os.Looper.loop(Looper.java:130)
02-27 01:25:19.419: E/AndroidRuntime(5326): at android.app.ActivityThread.main(ActivityThread.java:3835)
02-27 01:25:19.419: E/AndroidRuntime(5326): at java.lang.reflect.Method.invokeNative(Native Method)
02-27 01:25:19.419: E/AndroidRuntime(5326): at java.lang.reflect.Method.invoke(Method.java:507)
02-27 01:25:19.419: E/AndroidRuntime(5326): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
02-27 01:25:19.419: E/AndroidRuntime(5326): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
02-27 01:25:19.419: E/AndroidRuntime(5326): at dalvik.system.NativeStart.main(Native Method)
02-27 01:25:19.419: E/AndroidRuntime(5326): Caused by: android.content.ReceiverCallNotAllowedException: IntentReceiver components are not allowed to register to receive intents
02-27 01:25:19.419: E/AndroidRuntime(5326): at android.app.ReceiverRestrictedContext.registerReceiver(ContextImpl.java:151)
02-27 01:25:19.419: E/AndroidRuntime(5326): at android.app.ReceiverRestrictedContext.registerReceiver(ContextImpl.java:145)
02-27 01:25:19.419: E/AndroidRuntime(5326): at com.doublep.wakey.PowerConnectionReceiver.onReceive(PowerConnectionReceiver.java:46)
02-27 01:25:19.419: E/AndroidRuntime(5326): at android.app.ActivityThread.handleReceiver(ActivityThread.java:1915)
02-27 01:25:19.419: E/AndroidRuntime(5326): ... 10 more
You are getting a ReceiverCallNotAllowedException
.
This exception is thrown from
registerReceiver(BroadcastReceiver, IntentFilter)
andbindService(Intent, ServiceConnection, int)
when these methods are being used from an BroadcastReceiver component. In this case, the component will no longer be active upon returning from receiving the Intent, so it is not valid to use asynchronous APIs.
This means, that you can't register a BroadcastReceiver
inside a BroadcastReceiver
.
Edit:
Working example for a BroadcastReceiver, which listens to the ACTION_BATTERY_CHANGED
Intent:
private int mBatteryLevel;
private IntentFilter mBatteryLevelFilter;
BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
mBatteryLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
Toast.makeText(context, "Current Battery Level: " + mBatteryLevel, Toast.LENGTH_LONG).show();
}
};
private void registerMyReceiver() {
mBatteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(mBatteryReceiver, mBatteryLevelFilter);
}
Now you just have to call registerMyReceiver()
.
这篇关于电池改变广播接收器崩溃的应用程序在某些手机上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!