为什么广播接收器仅在华为设备上导致ANR?

问题描述 投票:0回答:3

我有一个广播接收器(声明清单),它监听意图并启动 jobIntentService。由于 jobIntentService 使用工作线程并且广播接收器没有任何繁重的操作(它只是接收意图并调用 jobIntentService)。广播接收器中不应有任何 ANR 原因。另外,我仅在华为设备上收到 ANR。

这是我的广播接收器代码:

public class SessionReceiver extends BroadcastReceiver {
    private static final String TAG = "SessionReceiver";
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction() == null) return;
        Log.d(TAG, "onReceive: called");
        SessionChangeService.enqueueWork(context, intent);
    }
}

如您所见,我只是将工作排队到广播接收器中,但我仍然在华为设备上收到 ANR。

舱单声明:

<receiver
    android:name=".receivers.SessionReceiver"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="android.media.action.OPEN_AUDIO_EFFECT_CONTROL_SESSION" />
        <action android:name="android.media.action.CLOSE_AUDIO_EFFECT_CONTROL_SESSION" />
    </intent-filter>
</receiver>

工作意向服务:

public class SessionChangeService extends JobIntentService {
    private static final String TAG = "SessionChangeService";

    public static void enqueueWork(Context context, Intent intent) {
        Log.d(TAG, "enqueueWork: enqueued");
        enqueueWork(context, SessionChangeService.class, Constants.SESSION_CHANGE_JOB_ID, intent);
    }

    @Override
    protected void onHandleWork(@NonNull Intent intent) {
           //...work...
    }
}

ANR日志是:

Broadcast of Intent { act=android.media.action.CLOSE_AUDIO_EFFECT_CONTROL_SESSION flg=0x2030 pkg=/.receivers.SessionReceiver (has extras) }

任何帮助将不胜感激。

android broadcastreceiver android-service crashlytics
3个回答
1
投票

您可能需要将您的应用程序添加到“受保护”应用程序列表,或允许在后台运行的应用程序列表。在许多低端设备以及小米、华为等设备上,Android 不会自动启动(或重新启动)应用程序,除非它们已明确添加到此列表中。查看“电池”、“电源管理”或“安全”下的“设置”。

请参阅以下内容了解更多信息:

BroadcastReceiver 工作一段时间,然后停止

华为手机“受保护应用程序”设置及处理方法

也可以试试

转到设置 -> 应用程序 -> 右上角菜单 -> 特殊访问 -> 电池优化。选择您的应用程序,然后单击“不允许”。


0
投票

根据上述分析,此问题与 SessionReceiver 无关,而应该是应用程序导航/ UI 中的某些内容阻塞了主线程。 随着主线程的阻塞,主线程中的广播接收者也会被阻塞,最终导致ANR。


0
投票

您可以检查主线程中应用程序初始化是否花费了太多时间。因此,接收器没有及时运行。 或者,您的应用程序不允许在后台运行。

© www.soinside.com 2019 - 2024. All rights reserved.