1

I have a Service that frequently updates the Main Activity UI by passing values via a LocalBroadCastManager. The following method is triggered within the Service to pass the value to the Main Activity:

private void updateUI(String statusValue){

        broadcastIntent.putExtra("status", statusValue);
        LocalBroadcastManager.getInstance(this).sendBroadcast(broadcastIntent);
    }

Within the Main Activity I added a BroadcastReceiver to pick up the value and update the UI accordingly:

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        // Get extra data included in the Intent
        String brStatus = intent.getStringExtra("status");

        if(brStatus != null){

            //Update UI 
            }
        }
    }
};

When the user navigates to another activity the receiver for the Broadcasts is unregistered as the user wont see the UI. Then onResume() when the user returns to the activity the receiver is reregistered:

LocalBroadcastManager.getInstance(this)
                .registerReceiver(mMessageReceiver, new IntentFilter("speed-stats"));

UPDATE: Whilst the activity is paused, the user can make actions (such as 'Pause') by clicking on Pending Intents on the ongoing notification. This action is handled within onStartCommand() of the Service:

case PAUSE_SERVICE :

                    Log.i(LOG_TAG, "Pause Foreground service.");

                    startForeground(NOTIF_ID,makeNotification(isRunning = false));

                    updateUI("paused");
                    stopSpeed();
                    break;

This works fine, however i have noticed that the UI is not updated as the receiver is unregistered whilst the activity is paused.

Is it possible to continue these UI updates despite pausing? Or is it possible to apply the UI updates as soon as the activity is resumed?

Basmatti
  • 27
  • 8

1 Answers1

0

When the user navigates to another activity the receiver for the Broadcasts is unregistered as the user wont see the UI.

This is the reason why your Activity's data is not up to date. Since the broadcast receiver is unregistered, data sent by the service won't be received.

Since its not a great idea keep the receiver registered, one solution would be:

  • Bind the service in on onResume() an unBind() it in onStop().
  • Inside service maintain data object, which will hold the latest data.
  • After service is binded, call the service method through Binder which will return the the data Object with latest data.
  • Update the data in your Activity accordingly.

You can refer this SO for binding/unbinding service

Sagar
  • 23,903
  • 4
  • 62
  • 62
  • Thanks for your reply. With your advice I have set it up that it is a Started Service and binds to the Main Activity `onStart()`. When bound, it calls a method `getState()` to update the state of a button (paused/running). This works, however i would also like to pass speed values continuously from the Service to the Main Activity when the service has been started. Is it possible to pass these values continuously? From what i see so far with it being bound only the activity can make requests to the service. – Basmatti Jun 10 '18 at 06:50
  • @InspectorNorse Service bound is for one time use only (get the current state after resume). Once the data is up to date, continue using the `LocalBroadcastReceiver` for future value updates and let service broadcast it. – Sagar Jun 10 '18 at 07:12