androID AsyncTask做下载进度条
AsyncTask是个不错的东西,可以使用它来做下载进度条。代码讲解如下:
package com.example.downloadfile; import java.io.file; import java.io.fileOutputStream; import java.io.inputStream; import java.net.httpURLConnection; import java.net.URL; import androID.app.Activity; import androID.app.Dialog; import androID.app.ProgressDialog; import androID.os.AsyncTask; import androID.os.Bundle; import androID.os.Environment; import androID.util.Log; import androID.Widget.TextVIEw; public class Downloadfile extends Activity { public static final String LOG_TAG = "test"; private ProgressDialog mProgressDialog; public static final int DIALOG_DOWNLOAD_PROGRESS = 0; file rootDir = Environment.getExternalStorageDirectory(); //定义要下载的文件名 public String filename = "test.jpg"; public String fileURL = "https://lh4.Googleusercontent.com/-HiJOyupc-tQ/TgnDx1_HDzI/AAAAAAAAAWo/DEeOtnRimak/s800/DSC04158.JPG"; @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); TextVIEw tv = new TextVIEw(this); tv.setText("AndroID Download file With Progress bar"); //检查下载目录是否存在 checkAndCreateDirectory("/mydownloads"); //执行asynctask new DownloadfileAsync().execute(fileURL); } class DownloadfileAsync extends AsyncTask<String,String,String> { @OverrIDe protected voID onPreExecute() { super.onPreExecute(); showDialog(DIALOG_DOWNLOAD_PROGRESS); } @OverrIDe protected String doInBackground(String... aurl) { try { //连接地址 URL u = new URL(fileURL); httpURLConnection c = (httpURLConnection) u.openConnection(); c.setRequestMethod("GET"); c.setDoOutput(true); c.connect(); //计算文件长度 int lenghtOffile = c.getContentLength(); fileOutputStream f = new fileOutputStream(new file(rootDir + "/my_downloads/",filename)); inputStream in = c.getinputStream(); //下载的代码 byte[] buffer = new byte[1024]; int len1 = 0; long total = 0; while ((len1 = in.read(buffer)) > 0) { total += len1; //total = total + len1 publishProgress("" + (int)((total*100)/lenghtOffile)); f.write(buffer,len1); } f.close(); } catch (Exception e) { Log.d(LOG_TAG,e.getMessage()); } return null; } protected voID onProgressUpdate(String... progress) { Log.d(LOG_TAG,progress[0]); mProgressDialog.setProgress(Integer.parseInt(progress[0])); } @OverrIDe protected voID onPostExecute(String unused) { //dismiss the dialog after the file was downloaded dismissDialog(DIALOG_DOWNLOAD_PROGRESS); } } public voID checkAndCreateDirectory(String dirname){ file new_dir = new file( rootDir + dirname ); if( !new_dir.exists() ){ new_dir.mkdirs(); } } @OverrIDe protected Dialog onCreateDialog(int ID) { switch (ID) { case DIALOG_DOWNLOAD_PROGRESS: //we set this to 0 mProgressDialog = new ProgressDialog(this); mProgressDialog.setMessage("Downloading file..."); mProgressDialog.setIndeterminate(false); mProgressDialog.setMax(100); mProgressDialog.setProgressstyle(ProgressDialog.STYLE_HORIZONTAL); mProgressDialog.setCancelable(true); mProgressDialog.show(); return mProgressDialog; default: return null; } } }
配置文件
注意打开文件保存权限
<?xml version="1.0" enCoding="utf-8"?> <manifest xmlns:androID="http://schemas.androID.com/apk/res/androID" package="com.example.downloadfile" androID:versionCode="1" androID:versionname="1.0"> <uses-permission androID:name="androID.permission.INTERNET" /> <uses-permission androID:name="androID.permission.WRITE_EXTERNAL_STORAGE" /> <uses-sdk androID:minSdkVersion="4" /> <application androID:icon="@drawable/icon" androID:label="@string/app_name"> <activity androID:name=".Downloadfile" androID:label="@string/app_name"> <intent-filter> <action androID:name="androID.intent.action.MAIN" /> <category androID:name="androID.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
总结以上是内存溢出为你收集整理的Android中使用AsyncTask做下载进度条实例代码全部内容,希望文章能够帮你解决Android中使用AsyncTask做下载进度条实例代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)