当前位置:   article > 正文

android 下载进度条_downloadprogress totalsize=-1

downloadprogress totalsize=-1

public class DownloadProgress extends Dialog {
 private Context ctx;
 
 /**文件总大小 */
 private int fileTotalSize;
 
 /**已经下载的 大小*/
 private int downLoadFileSize;
 
 /**已下载 百分比*/
 private String downloadPercent;

 private String downLoadedMB;
 
 private String totalMBSize;
 
 public static int K = 1024;
 private static final int MB = 1024 * 1024;
 
 public DecimalFormat dFormat;
 public ProgressBar pb;
 public TextView left;
 public TextView right;
 public Button cancelButton;
 private String msg;
 private boolean cancelFalg;
 
 private Handler handler = new Handler() {

  @Override
  public void handleMessage(Message msg) {
   // 定义一个Handler,用于处理下载线程与UI间通讯 
   if (!Thread.currentThread().isInterrupted()) {
    switch (msg.what) {
    case 0:
     pb.setMax(fileTotalSize);
    case 1:
     pb.setProgress(downLoadFileSize);
     left.setText(downloadPercent + "%");
     right.setText(downLoadedMB + "/" + totalMBSize);
     break;
    case 2:
     DownloadProgress.this.dismiss();
     new AlertDialog.Builder(ctx).setTitle("提示信息").setMessage("下载完成!").setNegativeButton("确定",
      new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int which) {
       }
      }).show();
     break;
    case -1:
     String error = msg.getData().getString("error");
     DialogHelper.showToastCanceledOnTouchOutside(ctx,null,error);
     break;
    }
   }
   super.handleMessage(msg);
  }

 };

 public DownloadProgress(Context context,boolean cancelFlag) {
  this(context,"",cancelFlag);
 }
 
 public DownloadProgress(Context context,String msg,boolean cancelFlag) {
  this(context,"正在下载",msg,cancelFlag);
 }
 
 public DownloadProgress(Context context,String title,String msg,boolean cancelFlag) {
  super(context);
  this.ctx = context;
  this.setCancelable(false);//按home键取消
  this.setTitle(title);
  this.msg = msg;
  this.cancelFalg = cancelFlag;
 }

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.download_progress);
  dFormat = (DecimalFormat) DecimalFormat.getInstance();
  dFormat.applyPattern("#0.0");
  //下载提示消息
  TextView msgView = (TextView)findViewById(R.id.progress_dialog_msg);
  if(!StringUtils.isEmpty(msg)) {
   msgView.setText(msg);
  }
  pb = (ProgressBar) findViewById(R.id.down_pb);
  left = (TextView) findViewById(R.id.leftText);
  right = (TextView) findViewById(R.id.rightText);
  if(cancelFalg){
   cancelButton = (Button) findViewById(R.id.btn_cancel); 
   cancelButton.setVisibility(View.VISIBLE);
   cancelButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
     DownloadProgress.this.setFinish();
     DownloadProgress.this.dismiss();
    }
   });
  }  
 }
 
 public void refresh(int downloadSize, int flag) {
  if (flag == 1) {
   downLoadFileSize = downloadSize;
   downloadPercent = dFormat.format(downloadSize * 100.0 / fileTotalSize);
   downLoadedMB = dFormat.format(downloadSize * 1.00 / MB) + "M";
  } else if (flag == 0) {
   fileTotalSize = downloadSize;
   totalMBSize = dFormat.format(fileTotalSize * 1.00 / MB) + "M";
  }
  Message msg = new Message();
  msg.what = flag;
  handler.sendMessage(msg);
 }

 public void setFinish() {
  pb.setProgress(fileTotalSize);
 }

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/283766
推荐阅读
相关标签
  

闽ICP备14008679号