3

我在 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() 引起的。

我发现一些资源建议您可以将传递给广播接收器的上下文转换为应用程序上下文,然后在应用程序上下文上调用 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()。即使使用显然应该允许这样做的应用程序上下文,该行为似乎也是未定义和不可预测的。

4

0 回答 0