BroadcastReceiver for Android Chooser not receiving anything

from the CommonsWare Community archives

At June 15, 2020, 5:40pm, sudokai asked:

I’m following the instructions from the official docs and I don’t understand why I don’t receive anything:

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
sendIntent.setType("file/*");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
    PendingIntent pi = PendingIntent.getBroadcast(getContext(), 0,
        new Intent(getContext(), FileExportBroadcastReceiver.class),
        PendingIntent.FLAG_UPDATE_CURRENT);
    startActivity(Intent.createChooser(sendIntent, null, pi.getIntentSender()));
}

Since implicits intents declared in the manifest no longer work after Android 8, I register the BroadcastReceiver in code:

class FileExportBroadcastReceiver : BroadcastReceiver() {
  override fun onReceive(context: Context?, intent: Intent?) {
    Log.d("FileExportBroadcastReceiver", "broadcast received")
  }
}
context.registerReceiver(new FileExportBroadcastReceiver(),
                new IntentFilter(Intent.ACTION_SEND));

But I never receive anything when I choose a share target??


At June 15, 2020, 6:02pm, mmurphy replied:

The broadcast is not ACTION_SEND. Instead, just register your receiver in the manifest. Your PendingIntent is set up for a manifest-registered receiver.