Search code examples
androidbroadcastreceiver

BroadcastReceiver goAsync() returns null PendingResult


I have a BroadcastReceiver in which I want to perform some work off the UI thread. As per the example in the BroadcastReceiver documentation, I first call goAsync(), which returns a PendingResult, then in my non-UI thread I call PendingResult.finish().

However, I get a NullPointerException because goAsync() returns null.

What am I doing wrong?

Here is a simple example that replicates the behaviour. I used targetSdkVersion 27 and minSdkVersion 23:

package com.example.broadcastreceiverpendingintenttest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        PendingResult pendingResult = goAsync();

        AsyncTask<Void, Void, Void> asyncTask = new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... voids) {
                Log.i("tag", "performed async task");
                pendingResult.finish();
                return null;
            }
        };

        asyncTask.execute();
    }
}

Here is a simple Activity that calls it:

package com.example.broadcastreceiverpendingintenttest;

import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    public static final String ACTION = "MY_ACTION";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(this);
        IntentFilter intentFilter = new IntentFilter(ACTION);
        broadcastManager.registerReceiver(new MyReceiver(), intentFilter);

        Button button = findViewById(R.id.button);
        button.setOnClickListener(v -> {
            broadcastManager.sendBroadcast(new Intent(ACTION));
        });
    }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.broadcastreceiverpendingintenttest.MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="232dp"
        android:layout_marginEnd="148dp"
        android:layout_marginStart="148dp"
        android:layout_marginTop="231dp"
        android:text="Press me"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

Solution

  • I wrote a JobIntentService instead:

    package com.example.broadcastreceiverpendingintenttest;
    
    import android.content.Intent;
    import android.support.annotation.NonNull;
    import android.support.v4.app.JobIntentService;
    import android.util.Log;
    
    public class MyJobIntentService extends JobIntentService {
    
        public static final String ACTION = "and....action!";
    
        static final int JOB_ID = 9001;
    
        @Override
        protected void onHandleWork(@NonNull Intent intent) {
            // TODO check the intent action is valid, get extras etc
            Log.i("tag", "performed my task");
        }
    }
    

    It's declared in AndroidManifest as follows:

    <service
        android:name=".MyJobIntentService"
        android:permission="android.permission.BIND_JOB_SERVICE"
        android:exported="false" />
    

    MyReceiver then enqueues work for the Service:

    package com.example.broadcastreceiverpendingintenttest;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.os.AsyncTask;
    import android.util.Log;
    
    public class MyReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
    
            Intent jobIntent = new Intent();
            jobIntent.setAction(MyJobIntentService.ACTION);
            MyJobIntentService.enqueueWork(context, MyJobIntentService.class, MyJobIntentService.JOB_ID, jobIntent);
        }
    }
    

    Bit more overhead than I wanted, but it gets the job done.