2

I'm using an Android foreground Service to download files and I'm showing the progress of the download to the user in a Notification.

I also have a progress bar in a fragment in the app. I'm using a broadcast Receiver to communicate the progress of the download happenning in the service to my fragment.

My problem is that the onReceive of my BroadcastReceiver is not trigger above Android 11 bu works perfectly fine on android 10, What had change about broadcast reveiver since Android 11 ?

On my fragment i'm declaring my broadcast receiver :

private final BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if ("SHOW_PROGRESS".equals(intent.getAction())) {
                showProgress(intent.getIntExtra("progress", 0));
            }
        }
    };

And registering it in OnCreateView :

 IntentFilter filter = new IntentFilter();
 filter.addAction("SHOW_PROGRESS");
 getContext().registerReceiver(receiver, filter);

From my service I'm calling it :

Intent broadcastIntent = new Intent().setAction("SHOW_PROGRESS").putExtra("progress", downLoadState.getGlobalPercent());
getApplicationContext().sendBroadcast(broadcastIntent);

I have nothing in my manifest since my service don't interacts with device components

loic .B
  • 281
  • 2
  • 11
  • 1
    Is the service in the same process as the fragment? If so, you do not need a `BroadcastReceiver` -- you can use in-process communications option, such as an RxJava subject or `LiveData`. Note that your current setup is fairly insecure, in that any app can both monitor your broadcasts and send fake broadcasts to your app. – CommonsWare Feb 06 '23 at 23:03
  • First I would try to make your intent "explicit", in your services intent builder add `.setPackage(getApplicationContext().getPackageName())` and see if that helps. – Pawel Feb 06 '23 at 23:14
  • Or at least use LocalBroadcasetManager to broadcast only inside Your app – bongo Feb 07 '23 at 06:29
  • @CommonsWare I launch the service in the fragment but once the service is started the user should be able to close the fragment or put the app in background so I thought exmplicit Broadcast Recevier was the best option. I tried replacing with LocalBroadcastManager but didn't help. Only changes in android 11 mentioned in the docs is about service stop working if it uses location or microphone, which is not my case.. – loic .B Feb 07 '23 at 11:28

0 Answers0