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);
}
}
...
}