菜单

事件式实时下载更新滚动条进程数据

2019年5月3日 - Java

 
关于下载更新数据于滚动条类似PrograssBar控件的难点,小编网络搜索了下,总体结合差不多选择微软的放大计时器Timer控件更新数据。

毫无疑问要定义3个 DownloadChangeObserver extends
ContentObserver,在其复写的
onChange()方法里实时第检查实验下载的情景,然后去做要求做的事务
@Override
public void onChange(boolean selfChange) {
//每一次 database变化时 就触发此事件,开首具体查询
Log.e(TAG, “onChange: –“);
// ProgressUtils.showProgress(context,”正在张开中,请稍后……”);
checkStatus();
}

 
在英特网开掘了拔尖的事例是使用TImer放大计时器和BackgroundWorker组件的重组更新PrograssBar进度的操作,网站如下:

private void checkStatus() {
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(downloadId);
//施行查询 再次回到3个Cursor
Cursor cursor = downloadManager.query(query);
if (cursor.moveToFirst()) {
int status =
cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
//文件地点
String fileLocalName =
cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
Log.e(TAG, “checkStatus:fileLocalName: ” + fileLocalName);

  http://www.cnblogs.com/jaxu/archive/2011/08/05/2128811.html

        // 已下载的字节大小
        long downloadedSoFar = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
        // 下载文件的总字节大小
        long totalSize = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
        Log.e(TAG, "checkStatus:downloadedSoFar " + downloadedSoFar + "  totalSize" + totalSize);

 
然则利用Timer放大计时器更新滚动条并非实时更新数据,顶七只是尽量模拟程序试行的步调,本篇小说选用另一种下载更新滚动条数据的不2法门来实现实时更新数据,结合WebService互连网服务和线程试行操作。

//
//
if(status!=DownloadManager.STATUS_FAILED&&downloadedSoFar<totalSize){
// Log.e(TAG, “status loading: —” );
// ProgressUtils.showProgress(context,”zhengzai—-“);
// }
switch (status) {
case DownloadManager.STATUS_PAUSED:
break;
case DownloadManager.STATUS_FAILED:
ProgressUtils.hideProgress();
ToastUtil.show(context, “下载文件退步,请重试”);
break;
case DownloadManager.STATUS_RUNNING:
ProgressUtils.showProgress(context, “正在展开中……”);
break;
case DownloadManager.STATUS_PENDING:
Log.e(TAG, “pending: “);
break;
case DownloadManager.STATUS_SUCCESSFUL:
ProgressUtils.hideProgress();
//获取下载文件的Uri
Uri downloadFileUri =
downloadManager.getUriForDownloadedFile(downloadId);
openFile(downloadFileUri);
// IntentUtils.getPdfFileIntent(file,context);
Log.e(TAG, “STATUS_SUCCESSFUL: —-pdfFile” + file.getName());
break;
default:
break;
}
}

}

  图片 2

图片 3

  (一)主程序入口:

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);
  //Application.Run(new Form1());
  System.Threading.Thread thread = new System.Threading.Thread(new
System.Threading.ThreadStart(UpdateLoading));
  thread.Start();
}

static void UpdateLoading()
{
  ConvMyReord.Uil.Welcome.DownLoader downLoader = new
Uil.Welcome.DownLoader();
  downLoader.Show();
  downLoader.Start();
}

  (2)DownLoader类:

private void downLoading_DownLoadingCancelEvnet()
{
  if (_downLoading != null && _downLoading.GetDialogResult ==
DialogResult.No)
  Application.Exit();
}

public void Start()
{
  if (_downLoading == null) _downLoading = new DownLoading();
  _downLoading.Dock = DockStyle.Fill;
  this.pnlMain.Controls.Add(_downLoading);
  _downLoading.DownLoadingCancelEvnet +=
downLoading_DownLoadingCancelEvnet;
  _downLoading.StartDownLoading();
}

  (3)自定义控件:DownLoading

图片 4

public delegate void DownLoadingCancel();
public event DownLoadingCancel DownLoadingCancelEvnet;
public DialogResult GetDialogResult {get;set;}

private void btnCancel_Click(object sender, EventArgs e)
{
  if(DownLoadingCancelEvnet != null)
  {
    this.GetDialogResult = DialogResult.No;
    DownLoadingCancelEvnet();
  }
}

public void StartDownLoading()
{
  ConvMyReord.WebReference.ConverRecordWebService
converRecordWebService
  = new WebReference.ConverRecordWebService();
  System.Data.DataSet ds =
converRecordWebService.DownLoadingScoure();//调用服务方法
  DownLoadHelper downLoadHelper = new DownLoadHelper();
  downLoadHelper.DownLoadProgressEvent +=
downloader_onDownLoadProgress;
  downLoadHelper.StartLoading(ds, this.progressBarControl1,
lblDownLoad);
}

//同步更新UI
private void downloader_onDownLoadProgress(long totalCount, long
current)
{
  float percent = 0;
  if (this.InvokeRequired)
  {
    this.Invoke(new
DownLoadHelper.DownLoadProgress(downloader_onDownLoadProgress), new
object[] { totalCount, current });
  }
  else
  {
    if (this.progressBarControl1.Properties.Maximum ==
this.progressBarControl1.Position)
    {
      this.GetDialogResult = DialogResult.Yes;
      this.btnCancel.Enabled = false;
    }
    this.progressBarControl1.Properties.Maximum = (int)totalCount;
    this.progressBarControl1.Position = (int)current;
    percent = (float)current / (float)totalCount * 100;
    this.lblDownLoad.Text = “当前补丁下载进程:” + percent.ToString()

  (4)DownLoadHelper帮助类:

public delegate void DownLoadProgress(long total, long current);
public event DownLoadProgress DownLoadProgressEvent;

public void StartLoading(System.Data.DataSet ds,
DevExpress.XtraEditors.ProgressBarControl progressBar1,
System.Windows.Forms.Label label1)
{
  if (ds == null) return;
  long totalCount = GetRowCounts(ds);
  LoadingData(totalCount, ds, progressBar1, label1);
}

private static long GetRowCounts(System.Data.DataSet ds)
{
  long count = 0;
  if (ds.Tables.Count <= 0) return count;
  for (int i = 0; i < ds.Tables.Count; i++)
  {
    System.Data.DataTable table = ds.Tables[i];
    count += table.Rows.Count;
  }
  return count;
}

public void LoadingData(long totalCount, System.Data.DataSet ds,
DevExpress.XtraEditors.ProgressBarControl progressBar1,
System.Windows.Forms.Label label1)
{
  if (totalCount <= 0 || ds.Tables.Count <= 0) return;
  long count = 0;
  for (int i = 0; i < ds.Tables.Count; i++)
  {
    System.Data.DataTable table = ds.Tables[i];
    if (table == null || table.Rows.Count <= 0) continue;
    for (int j = 0; j < table.Rows.Count; j++)
    {
      ++count;
      System.Windows.Forms.Application.DoEvents();
      if (DownLoadProgressEvent != null)
      DownLoadProgressEvent(100, (100/totalCount) * count);
       System.Threading.Thread.Sleep(100);
    }
  }
}

效果图:

图片 5

 

 
以上是自己前1个月本人编的一部分程序,挂念从内部存款和储蓄器空间和频率上都存在不足,本篇首假若让大家领会事件式实时更新滚动条数据的编制程序思路,不足之处,请留言赐教。
  A young idler  ~  an old beggar !

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图