Show Progressbar Or Dialog From An Intentservice For Download Progress
I have an activity with a 'download' button which fires up DownloadManager implemented in an IntentService. Everything is working just fine and my question is: Is it possible to di
Solution 1:
Is it possible to display ProgressBar or ProgressDialog from my DownloadService (which is extended IntentService), except the progress shown in the Notification bar?
Could you write a sample code or pseudo code how I can do that? Thank you
You can use ResultReceiver to reach your goal. ResultReceiver implements Parcelable so you are able to pass it into IntentService like:
Intent i = newIntent(this, DownloadService.class);
i.putExtra("receiver", newDownReceiver(newHandler()));
<context>.startService(i);
Then in your onHandlerIntent() all what you need is to obtain receiver you passed into Intent and send current progress into ResultReceiver:
protectedvoidonHandleIntent(Intent intent) {
// obtaining ResultReceiver from Intent that started this IntentServiceResultReceiverreceiver= intent.getParcelableExtra("receiver");
...
// data that will be send into ResultReceiverBundledata=newBundle();
data.putInt("progress", progress);
// here you are sending progress into ResultReceiver located in your Activity
receiver.send(Const.NEW_PROGRESS, data);
}
And ResultReceiverwill handle data and will make update inProgressDialog. Here is implementation of ResultReceiver (make it as inner class of your Activity class):
privateclassDownReceiverextendsResultReceiver {
publicDownloadReceiver(Handler handler) {
super(handler);
}
@OverridepublicvoidonReceiveResult(int resultCode, Bundle resultData) {
super.onReceiveResult(resultCode, resultData);
if (resultCode == Const.NEW_PROGRESS) {
intprogress= resultData.getInt("progress");
// pd variable represents your ProgressDialog
pd.setProgress(progress);
pd.setMessage(String.valueOf(progress) + "% downloaded sucessfully.");
}
}
}
Post a Comment for "Show Progressbar Or Dialog From An Intentservice For Download Progress"