1

I have an activity A which has a progressbar and a textview.

If user clicks a button a service is being started (ServiceB), I am trying to find a way how to update the progressbar in Activity A from the ServiceB and at the same time set the (progress) text in the Textview in Activity A.

I looked around on Google and Stackoverflow and I think I found a way to do it as described here

but I am having difficulties to implement this, any help is highly appreciated.

PS: don`t downvote, I know UI should not be directly accessed from a Service, so I am looking for a way to do it right.

Some relevant code:

Activity A:

@EActivity(R.layout.downloads_activity)
public class DownloadsActivity extends BaseActivity {

@ViewById(R.id.progress_text)
TextView progresstxt;

@ViewById(R.id.progressdownload)
ProgressBar downloadprogress;

// Update Progressbar and set Text sent from ServiceB
}

ServiceB:

public class ServiceB extends IntentService {
...

@Override
    public void onProgress(DownloadRequest request, long totalBytes, long downloadedBytes, int progress) {
        int id = request.getDownloadId();

        if (!isActive) {
            downloadManager.cancel(downloadId1);
            deleteCancelledFile.deleteOnExit();
        } else if (id == downloadId1) {
            // How to update progressbar and textview of Activity A?
            progresstxt.setText("Downloading: " + progress + "%" + "  " + getBytesDownloaded(progress, totalBytes));
            downloadprogress.setProgress(progress);
        }
    }
    ...
}
Community
  • 1
  • 1
Simon
  • 1,691
  • 2
  • 13
  • 28

1 Answers1

6

You need to use LocalBroadcastManager Following are the steps which needs to be taken care

Create a LocalBroadcastManager inside activity.

private BroadcastReceiver mLocalBroadcast = new BroadcastReceiver() {
  @Override
  public void onReceive(Context context, Intent intent) {
    // take values from intent which contains in intent if you putted their
    // here update the progress bar and textview 
    String message = intent.getStringExtra("message");
      int progress = Integer.parseInt(intent.getStringExtra("progress"));
  }
};

Register it on activity's onCreate()

  LocalBroadcastManager.getInstance(this).registerReceiver(mLocalBroadcast ,
      new IntentFilter("myBroadcast"));

UnRegister in activity's onDestroy()

// Unregister since the activity is about to be closed. LocalBroadcastManager.getInstance(this).unregisterReceiver(mLocalBroadcast );

Send updates from service to activity to update UI

From IntentService send progress and textView update via intent

Intent intent = new Intent("myBroadcast");
  // You can also include some extra data.
  intent.putExtra("message", "This is my message!"); // msg for textview if needed
  intent.putExtra("progress", progressValue); // progress update
  LocalBroadcastManager.getInstance(this).sendBroadcast(intent);

It will send these data to mLocalBroadcast which we register inside activity

Hope these help you.

Ajay Mistry
  • 951
  • 1
  • 14
  • 30
Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147