BroadcastReceiver中的ReceiverCallNotAllowedException使用应用程序上下文

时间:2015-12-23 15:29:20

标签: android broadcastreceiver android-broadcast android-broadcastreceiver

我在Android版本4.1.2上收到以下错误:

Fatal Exception: java.lang.RuntimeException: Unable to start receiver     com.example.PluggedInBroadcastReceiver: 
android.content.ReceiverCallNotAllowedException: IntentReceiver components are not allowed to register to receive intents
   at android.app.ActivityThread.handleReceiver(ActivityThread.java:2287)
   at android.app.ActivityThread.access$1600(ActivityThread.java:143)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:4961)
   at java.lang.reflect.Method.invokeNative(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
   at dalvik.system.NativeStart.main(NativeStart.java)
Caused by android.content.ReceiverCallNotAllowedException: IntentReceiver components are not allowed to register to receive intents
   at android.app.ReceiverRestrictedContext.registerReceiver(ContextImpl.java:152)
   at android.app.ReceiverRestrictedContext.registerReceiver(ContextImpl.java:146)
   at com.example.PluggedInBroadcastReceiver.getBatteryLevel(Unknown Source)
   at com.example.PluggedInBroadcastReceiver.onReceive(Unknown Source)
   at android.app.ActivityThread.handleReceiver(ActivityThread.java:2280)
   at android.app.ActivityThread.access$1600(ActivityThread.java:143)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:4961)
   at java.lang.reflect.Method.invokeNative(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
   at dalvik.system.NativeStart.main(NativeStart.java)

以下是导致错误的代码。此接收器是静态注册的(在AndroidManifest.xml中)而不是动态注册:

public class PluggedInBroadcastReceiver extends WakefulBroadcastReceiver {

public static final String LOG_TAG = PluggedInBroadcastReceiver.class.getSimpleName();

private IntentFilter iFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);

@Override
public void onReceive(Context context, Intent intent) {
    if (intent != null) {
        String action = intent.getAction();
        if (action != null && action.equals(Intent.ACTION_POWER_CONNECTED)) {
            try {
                Intent batteryStatusIntent = context.getApplicationContext().registerReceiver(null, iFilter);
                double battPercentage = BatteryUtils.getBatteryLevel(batteryStatusIntent);
            //Do something with the battery percentage value...    
            } catch (Exception e) {
                Log.e(LOG_TAG, "Battery status intent was null");
            }
        }
    }
 }
}

我在过去遇到过这个错误,发现我的问题是由传递给广播接收器的上下文无法调用registerReceiver()的事实引起的。

我发现一些resources表明您可以将传递给广播接收器的上下文转换为应用程序上下文,然后在应用程序上下文中调用registerReceiver()。这适用于Android版本4.2及更高版本,但在4.1.2上失败(可能更低,但未明确测试)。似乎转换到应用程序上下文应该允许调用registerReceiver(),但这似乎并不适用于所有版本的Android。

有没有人对此行为有解释?

编辑: 情节变得浓厚......我现在已经知道这个崩溃发生在Android版本4.1.1,4.1.2,4.3.0和5.1.1上,超过93%的崩溃发生在4.1.2上。我不确定我们是否会为此找到解决方案,但我的解决办法是永远不要从BroadcastReceiver调用registerReceiver()。即使使用显然应该允许这种情况的应用程序上下文,行为似乎也是未定义和不可预测的。

0 个答案:

没有答案