从IntentService显示ProgressBar或对话框以获取下载进度 [英] show ProgressBar or Dialog from an IntentService for download progress

查看:83
本文介绍了从IntentService显示ProgressBar或对话框以获取下载进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有下载"按钮的活动,该按钮启动了IntentService中实现的DownloadManager.一切正常,我的问题是:

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:

除了通知栏中显示的进度外,是否可以从我的DownloadService(扩展了IntentService)中显示ProgressBar或ProgressDialog?

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

推荐答案

是否可以从我的计算机上显示ProgressBar或ProgressDialogDownloadService(已扩展为IntentService),但进度除外显示在通知栏中?

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

您可以使用ResultReceiver达到目标.ResultReceiver实现了Parcelable,因此您可以像以下那样将其传递到IntentService中:

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

然后在您的 onHandlerIntent()中,您需要获取传递给Intent的接收器并将当前进度发送到ResultReceiver:

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

然后 ResultReceiver 将处理数据并在 ProgressDialog 中进行更新.这是ResultReceiver的实现(将其作为Activity类的内部类):

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

这篇关于从IntentService显示ProgressBar或对话框以获取下载进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆