gpt4 book ai didi

android - 某些手机​​上的电池已更换广播接收器崩溃应用程序

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:50:34 27 4
gpt4 key购买 nike

只要手机接通电源,我就会激活我的应用程序。这是我的 list

<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>

这是我的广播接收器

  @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);

现在,导致我 friend 手机崩溃的线路(在我的手机上一切正常)就是这条线路

Intent batteryStatus = context.registerReceiver(null, ifilter);

谁能告诉我是什么原因以及如何解决?我的手机是装有最新安卓 JB 的 galaxy nexus,他的手机是带有 Gingerbread 的 LG optimus

这是日志

    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

最佳答案

你得到一个 ReceiverCallNotAllowedException .

This exception is thrown from registerReceiver(BroadcastReceiver,
IntentFilter)
and bindService(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.

这意味着,您不能在 BroadcastReceiver 中注册 BroadcastReceiver

编辑:BroadcastReceiver 的工作示例,它监听 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);
}

现在您只需调用 registerMyReceiver()

关于android - 某些手机​​上的电池已更换广播接收器崩溃应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15102124/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com