1

I have a broadcast receiver(Manifest declared) that listens to an intent and starts the jobIntentService. Since jobIntentService is using Worker Threads and Broadcast Receiver does not have any heavy operations (It just receives the intent and calls the jobIntentService). There shouldn't be any reasons for ANRs in the broadcast receiver. Also, I'm getting ANRs only on Huawei devices.

enter image description here

enter image description here

Here's my code for broadcast receiver:

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

As you can see, I'm only enqueuing the work in the broadcast receiver but I'm still getting ANRs on Huawei devices.

Manifest declaration:

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

JobIntentService:

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 log is:

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

Any help will be appreciated.

Jazib Khan
  • 408
  • 4
  • 13

2 Answers2

1

You probably need to add your app to the list of "protected" apps, or the list of apps that are allowed to run in the background. On many low-end devices, and devices from Xiaomi, Huawei, etc. Android will not start (or restart) apps automatically unless they have been explicitly added to this list. Look in Settings under Battery, Power Management or Security.

See the following for more info:

BroadcastReceiver works for a while, then stops

"Protected Apps" setting on Huawei phones, and how to handle it

Also, you can try

GO to settings -> Apps -> Menu on right top corner -> Special access -> Battery Optimization. Select your app , and click on "Don't Allow" .

0

Based on the above analyses, this issue is not related to SessionReceiver but should be something in the app navigations/ UI that blocks the main thread. With the main thread blocked, broadcast receivers will also be blocked in the main thread and finally cause ANR.

Qumber Abbas
  • 560
  • 1
  • 4
  • 11