如何在Android中显示进度条中的下载进度?

如何在Android中显示进度条中的下载进度?,第1张

概述我正在使用.net网络服务,我可以在SD卡中下载文件,但我想在特定文件开始下载和完成时显示进度,然后我想显示视图和取消等选项. 点击事件类: public void onItemClick(AdapterView<?> parent, View v, int position, long id){ LazyAdapter ca = (LazyAdapter)parent.getAdap 我正在使用.net网络服务,我可以在SD卡中下载文件,但我想在特定文件开始下载和完成时显示进度条,然后我想显示视图和取消等选项.

点击事件类:

public voID onItemClick(AdapterVIEw<?> parent,VIEw v,int position,long ID){    LazyAdapter ca = (LazyAdapter)parent.getAdapter();    FolderList item_name = (FolderList)ca.getItem(position);    FolderList documentID = (FolderList)ca.getItem(position);    FolderList type = (FolderList)ca.getItem(position);    Intent mIntent = new Intent();      mIntent.putExtra("item_name",item_name.folder_name);    mIntent.putExtra("item_ID",documentID.ID);    mIntent.putExtra("item_type",type.type);    mIntent.getStringExtra("item_name");    String Type = mIntent.getStringExtra("item_type");    Log.i("Type",Type);    if {       // Some code here...    } else {        Intent i = new Intent(getApplicationContext(),display_image.class);        i.putExtra("item_name",item_name.folder_name);        i.putExtra("ID",documentID.ID);        i.putExtra("item_type",type.type);        i.putExtra("User_ID",User_ID);        i.getStringExtra("item_name");        ID = i.getStringExtra("ID");        i.getStringExtra("item_type");        startActivity(i);    }}

我的代码:我想使用下载管理器

SoapPrimitive documentResponse = (SoapPrimitive)Envelope.getResponse();        Log.i("documentResponse",documentResponse.toString());        String DocAsstring = documentResponse.toString();        byte[] decodedString = Base64.decode(DocAsstring,Base64.DEFAulT);         file direct = new file(Environment.getExternalStorageDirectory() + "/MyFolder");         if(!direct.exists())         {           direct.mkdir();         }        file photo = new file(Environment.getExternalStorageDirectory() + "/MyFolder",name);         if (photo.exists())         {           photo.delete();         }     try {         DownloadManager.Request request = new DownloadManager.Request(Uri.parse(name));         request.setDescription("Have Fun ;)");         request.setTitle("Downloading...");         // in order for this if to run,you must use the androID 3.2 to compile your app         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {             request.allowScanningByMediaScanner();             request.setNotificationVisibility(DownloadManager.Request.VISIBIliTY_VISIBLE_NOTIFY_COMPLETED);         }       fileOutputStream fos=new fileOutputStream(photo.getPath());       fos.write(decodedString);       fos.close();       DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);       manager.enqueue(request);       ContentValues values= new ContentValues();       System.out.println(values);     }     catch (java.io.IOException e)      {       Log.e("PictureDemo","Exception in photoCallback",e);     }    }

请建议如何使用下载管理器?谢谢

解决方法 您可以使用Progressbar来完成此任务.

首先,在您的界面中添加一个进度条,如下所示:

<Progressbar    androID:ID="@+ID/progress_bar"    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content"    />

然后在您的代码中更新您的方法,如下所示:

protected Progressbar mProgressbar;protected long downloadID;protected DownloadManager manager;public voID startdownload() {    //<SET UP DOWNLOAD MANAGER HERE>    downloadID = manager.enqueue(request);    mProgressbar = findVIEwByID(R.ID.progress_bar);    Timer myTimer = new Timer();    myTimer.schedule(new TimerTask() {                  @OverrIDe            public voID run() {            DownloadManager.query q = new DownloadManager.query();            q.setFilterByID(downloadID);            Cursor cursor = manager.query(q);            cursor.movetoFirst();            int bytes_downloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.ColUMN_BYTES_DOWNLOADED_SO_FAR));            int bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.ColUMN_TOTAL_SIZE_BYTES));            cursor.close();            int dl_progress = (bytesDownloaded * 1f / bytesTotal) * 100;            runOnUiThread(new Runnable(){                @OverrIDe                public voID run(){                    mProgressbar.setProgress((int) dl_progress);                }            });            }    },10);}
总结

以上是内存溢出为你收集整理的如何在Android中显示进度条中的下载进度?全部内容,希望文章能够帮你解决如何在Android中显示进度条中的下载进度?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1134606.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存