4

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 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

venta7
  • 169
  • 5
  • 13
  • you can use async task for showing the progress dialog, put you download service inside doinbackground() and in pre execute show dialog and post dialog cancel dialog. – Smogger Nov 28 '13 at 12:26
  • @ShashankAgarwal I should clarify that I would like to have my download service available also for other activities. The way you're proposing can I have access to download service from some other activity? – venta7 Nov 28 '13 at 12:32
  • **"Is it possible to display...except the progress shown in the Notification bar?"** - Yes it's possible. **"Could you write a sample code or pseudo code how I can do that?"** No. We don't write code for people here on Stackoverflow. I suggest you look at the android developers site and/or search the web for examples. There are plenty out there. – Squonk Nov 28 '13 at 12:33
  • you can put the asynctask in all activities in which you want that download services. – Smogger Nov 28 '13 at 12:35

1 Answers1

23

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 = new Intent(this, DownloadService.class);
i.putExtra("receiver", new DownReceiver(new Handler()));
<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:

protected void onHandleIntent(Intent intent) {  

   // obtaining ResultReceiver from Intent that started this IntentService
   ResultReceiver receiver = intent.getParcelableExtra("receiver");

   ...

   // data that will be send into ResultReceiver
   Bundle data = new Bundle();
   data.putInt("progress", progress);

   // here you are sending progress into ResultReceiver located in your Activity
   receiver.send(Const.NEW_PROGRESS, data);
}

And ResultReceiver will handle data and will make update in ProgressDialog. Here is implementation of ResultReceiver (make it as inner class of your Activity class):

private class DownReceiver extends ResultReceiver {

   public DownloadReceiver(Handler handler) {
      super(handler);
   }

   @Override
   public void onReceiveResult(int resultCode, Bundle resultData) {
      super.onReceiveResult(resultCode, resultData);
      if (resultCode == Const.NEW_PROGRESS) {
         int progress = resultData.getInt("progress");

         // pd variable represents your ProgressDialog
         pd.setProgress(progress);
         pd.setMessage(String.valueOf(progress) + "% downloaded sucessfully.");
      }
   }
}
Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
  • Reason of downvote Mr. downvoter? This works because im using it in my project. – Simon Dorociak Nov 28 '13 at 12:38
  • The OP is asking how to publish progress using a Notification. That requires using RemoteViews. What makes you think the OP is starting the IntentService from an Activity? I trigger one of mine from either an Alarm created with AlarmManager or from a BroadcastReceiver, for example. In neither case would a ResultReceiver be suitable. – Squonk Nov 28 '13 at 12:41
  • 2
    @Squonk Did you read question carefully? "Is it possible to **display ProgressBar or ProgressDialog from my DownloadService** (which is extended IntentService), **except** the progress shown in the Notification bar?" – Simon Dorociak Nov 28 '13 at 12:43
  • @Geralt I'll try this, thank you. I've searched online how to accomplish this but couldn't find anything that explains how to do it from IntentService. One I have on mind is to pass the download ID to a popup activity which will handle the progress-bar/dialog – venta7 Nov 28 '13 at 12:47
  • @venta7 so you have activity so all what you need is to create inner class that will represent ResultReciever. When you tap on button, you are starting service, here you will "append" your receiver into Intent that starts IntentService and other code shows how to obtain in IntentService that receiver and how to send progress back to your activity. – Simon Dorociak Nov 28 '13 at 12:51
  • @Geralt : I've removed my downvote. But to clarify - as a native English speaker (as you've correctly surmised) the wording of the OP's question is actually ambiguous and could be interpreted either way. I interpreted it one way and you interpreted it the other (which based on the OP's comment above was obviously the correct way). The biggest problem with the English language is a comma in the wrong place can make a sentence have a completely different meaning. I apologise. – Squonk Nov 28 '13 at 13:00
  • @venta7 i slightly updated answer and i did add some comments to code to make thing more clear. – Simon Dorociak Nov 28 '13 at 13:00
  • @Squonk No problem buddy, you explained that, no problem. I normally asked why downvote - that is normal for someone who cares about answers and reasons why upvote and why downvote. When i'll downvote someone, i also add comment why i downvoted him so its ok. i wasn't angry (it maybe looked like it) but just wanted to know why downvote. Have a nice day. – Simon Dorociak Nov 28 '13 at 13:03
  • @Geralt just to clarify - do you use DownloadManager in your IntentService or you handle the connection and download manually? I'm asking because I'm not sure how to obtain 'total' and 'length' variables via DownloadManager. – venta7 Nov 28 '13 at 14:32
  • @venta7 i used connection but in your case can obtain progress from service, its? So you don't need lenth and total. I'm going to update answer to be not confused. – Simon Dorociak Nov 28 '13 at 14:52
  • what is progress holding here ? @SimonDorociak – Jay Rathod Mar 13 '23 at 11:30