最近做项目卡壳了,要做个Android的应用市场,其他方面都还好说,唯独这个下载管理算是给我难住了,究其原因,一是之前没有做过类似的功能,二是这个项目催的着实的急促,以至于都没什么时间能仔细研究这方面的内容,三是我这二把刀的基本功实在是不太扎实啊。不过好在经高人指点,再加上bing以及stackoverflow的帮助,好歹算是有些成果,下面就将这小小的成果分享一下,虽然是使用的AsyncTask来完成,但是个人觉得还是service要更靠谱些,不过那个得等有空儿再研究了。
AsyncTask是何物我就不再赘述了,度娘,谷哥,必应都会告诉你的,不过建议大家看看文章最后参考资料的第二个链接,写的还是非常详细的。我认为它实际上就是个简单的迷你的Handler,反正把一些异步操作扔给它以后,就只需要等着它执行完就齐活了。
那么怎么用这玩意儿实现一个下载管理的功能?大体的思路是这样的:
1.点击下载按钮以后,除了要让AsyncTask开始执行外,还要把下载的任务放到HashMap里面保存,这样做的好处就是能够在列表页进行管理,比如暂停、继续下载、取消。
2.下载管理页的列表,使用ScrollView,而非ListView。这样做的好处就是为了能方便的更新ProgressBar进度。
那咱先来说说启动下载任务。
- btnDownload.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- String url = datas.get(position).get("url");
- Async asyncTask = null; // 下载renwu
- boolean isHas = false;
- // 判断当前要下载的这个连接是否已经正在进行,如果正在进行就阻止在此启动一个下载任务
- for (String urlString : AppConstants.listUrl) {
- if (url.equalsIgnoreCase(urlString)) {
- isHas = true;
- break;
- }
- }
- // 如果这个连接的下载任务还没有开始,就创建一个新的下载任务启动下载,并这个下载任务加到下载列表中
- if(isHas == false) {
- asyncTask = new Async(); // 创建新异步
- asyncTask.setDataMap(datas.get(position));
- asyncTask.setContext(context);
- AppConstants.mapTask.put(url, asyncTask);
- // 当调用AsyncTask的方法execute时,就会去自动调用doInBackground方法
- asyncTask.executeOnExecutor(Executors.newCachedThreadPool(), url);
- }
- }
- });
这里我为什么写asyncTask.executeOnExecutor(Executors.newCachedThreadPool(), url);而不是asyncTask.execute(url);呢?先卖个关子,回头咱再说。
下面来看看Async里都干了啥。
- package com.test.muldownloadtest.task;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.RandomAccessFile;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.HashMap;
- import com.test.muldownloadtest.AppConstants;
- import com.test.muldownloadtest.bean.DBHelper;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.os.AsyncTask;
- import android.os.Environment;
- import android.os.Handler;
- import android.os.Message;
- import android.widget.ListView;
- import android.widget.ProgressBar;
- import android.widget.TextView;
- // AsyncTask<Params, Progress, Result>
- public class Async extends AsyncTask<String, Integer, String> {
- /* 用于查询数据库 */
- private DBHelper dbHelper;
- // 下载的文件的map,也可以是实体Bean
- private HashMap<String, String> dataMap = null;
- private Context context;
- private boolean finished = false;
- private boolean paused = false;
- private int curSize = 0;
- private int length = 0;
- private Async startTask = null;
- private boolean isFirst = true;
- private String strUrl;
- @Override
- protected String doInBackground(String... Params) {
- dbHelper = new DBHelper(context);
- strUrl = Params[0];
- String name = dataMap.get("name");
- String appid = dataMap.get("appid");
- int startPosition = 0;
- URL url = null;
- HttpURLConnection httpURLConnection = null;
- InputStream inputStream = null;
- RandomAccessFile outputStream = null;
- // 文件保存路径
- String path = Environment.getExternalStorageDirectory().getPath();
- // 文件名
- String fileName = strUrl.substring(strUrl.lastIndexOf('/'));
- try {
- length = getContentLength(strUrl);
- startPosition = getDownloadedLength(strUrl, name);
- /** 判断是否是第一次启动任务,true则保存数据到数据库并下载,
- * false则更新数据库中的数据 start
- */
- boolean isHas = false;
- for (String urlString : AppConstants.listUrl) {
- if (strUrl.equalsIgnoreCase(urlString)) {
- isHas = true;
- break;
- }
- }
- if (false == isHas) {
- saveDownloading(name, appid, strUrl, path, fileName, startPosition, length, 1);
- }
- else if (true == isHas) {
- updateDownloading(curSize, name, strUrl);
- }
- /** 判断是否是第一次启动任务,true则保存数据到数据库并下载,
- * false则更新数据库中的数据 end
- */
- // 设置断点续传的开始位置
- url = new URL(strUrl);
- httpURLConnection = (HttpURLConnection)url.openConnection();
- httpURLConnection.setAllowUserInteraction(true);
- httpURLConnection.setRequestMethod("GET");
- httpURLConnection.setReadTimeout(5000);
- httpURLConnection.setRequestProperty("User-Agent","NetFox");
- httpURLConnection.setRequestProperty("Range", "bytes=" + startPosition + "-");
- inputStream = httpURLConnection.getInputStream();
- File outFile = new File(path+fileName);
- // 使用java中的RandomAccessFile 对文件进行随机读写操作
- outputStream = new RandomAccessFile(outFile,"rw");
- // 设置开始写文件的位置
- outputStream.seek(startPosition);
- byte[] buf = new byte[1024*100];
- int read = 0;
- curSize = startPosition;
- while(false == finished) {
- while(true == paused) {
- // 暂停下载
- Thread.sleep(500);
- }
- read = inputStream.read(buf);
- if(read==-1) {
- break;
- }
- outputStream.write(buf,0,read);
- curSize = curSize+read;
- // 当调用这个方法的时候会自动去调用onProgressUpdate方法,传递下载进度
- publishProgress((int)(curSize*100.0f/length));
- if(curSize == length) {
- break;
- }
- Thread.sleep(500);
- updateDownloading(curSize, name, strUrl);
- }
- if (false == finished) {
- finished = true;
- deleteDownloading(strUrl, name);
- }
- inputStream.close();
- outputStream.close();
- httpURLConnection.disconnect();
- }
- catch (MalformedURLException e) {
- e.printStackTrace();
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- catch (InterruptedException e) {
- e.printStackTrace();
- }
- finally {
- finished = true;
- deleteDownloading(strUrl, name);
- if(inputStream!=null) {
- try {
- inputStream.close();
- if(outputStream!=null) {
- outputStream.close();
- }
- if(httpURLConnection!=null) {
- httpURLConnection.disconnect();
- }
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- // 这里的返回值将会被作为onPostExecute方法的传入参数
- return strUrl;
- }
- /**
- * 暂停下载
- */
- public void pause() {
- paused = true;
- }
- /**
- * 继续下载
- */
- public void continued() {
- paused = false;
- }
- /**
- * 停止下载
- */
- @Override
- protected void onCancelled() {
- finished = true;
- deleteDownloading(dataMap.get("url"), dataMap.get("name"));
- super.onCancelled();
- }
- /**
- * 当一个下载任务成功下载完成的时候回来调用这个方法,
- * 这里的result参数就是doInBackground方法的返回值
- */
- @Override
- protected void onPostExecute(String result) {
- try {
- String name = dataMap.get("name");
- System.out.println("name===="+name);
- // 判断当前结束的这个任务在任务列表中是否还存在,如果存在就移除
- if (AppConstants.mapTask.containsKey(result)) {
- if (AppConstants.mapTask.get(result) != null) {
- finished = true;
- deleteDownloading(result, name);
- }
- }
- }
- catch (NumberFormatException e) {
- e.printStackTrace();
- }
- super.onPostExecute(result);
- }
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- }
- /**
- * 更新下载进度,当publishProgress方法被调用的时候就会自动来调用这个方法
- */
- @Override
- protected void onProgressUpdate(Integer... values) {
- super.onProgressUpdate(values);
- }
- /**
- * 获取要下载内容的长度
- * @param urlString
- * @return
- */
- private int getContentLength(String urlString){
- try {
- URL url = new URL(urlString);
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- return connection.getContentLength();
- }
- catch (MalformedURLException e) {
- e.printStackTrace();
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- return 0;
- }
- /**
- * 从数据库获取已经下载的长度
- * @param url
- * @param name
- * @return
- */
- private int getDownloadedLength(String url, String name) {
- int downloadedLength = 0;
- SQLiteDatabase db = dbHelper.getReadableDatabase();
- String sql = "SELECT downloadBytes FROM fileDownloading WHERE downloadUrl=? AND name=?";
- Cursor cursor = db.rawQuery(sql, new String[] { url, name });
- while (cursor.moveToNext()) {
- downloadedLength = cursor.getInt(0);
- }
- db.close();
- return downloadedLength;
- }
- /**
- * 保存下载的数据
- * @param name
- * @param appid
- * @param url
- * @param downloadedLength
- */
- private void saveDownloading(String name, String appid, String url, String savePath, String fileName, int downloadBytes, int totalBytes, int status) {
- SQLiteDatabase db = dbHelper.getWritableDatabase();
- try {
- db.beginTransaction();
- String sql = "INSERT INTO fileDownloading(name, appid, downloadUrl, savePath, fileName, downloadBytes, totalBytes, downloadStatus) " +
- "values(?,?,?,?,?,?,?,?)";
- db.execSQL(sql, new Object[]{ name, appid, url, savePath, fileName, downloadBytes, totalBytes, status});
- db.setTransactionSuccessful();
- boolean isHas = false;
- // 判断当前要下载的这个连接是否已经正在进行,如果正在进行就阻止在此启动一个下载任务
- for (String urlString : AppConstants.listUrl) {
- if (url.equalsIgnoreCase(urlString)) {
- isHas = true;
- break;
- }
- }
- if (false == isHas) {
- AppConstants.listUrl.add(url);
- }
- if (false == isFirst) {
- AppConstants.mapTask.put(url, startTask);
- }
- }
- finally {
- db.endTransaction();
- db.close();
- }
- }
- /**
- * 更新下载数据
- * @param cursize
- * @param name
- * @param url
- */
- private void updateDownloading(int cursize, String name, String url) {
- SQLiteDatabase db = dbHelper.getWritableDatabase();
- try {
- db.beginTransaction();
- String sql = "UPDATE fileDownloading SET downloadBytes=? WHERE name=? AND downloadUrl=?";
- db.execSQL(sql, new String[] { cursize + "", name, url });
- db.setTransactionSuccessful();
- } finally {
- db.endTransaction();
- db.close();
- }
- }
- /**
- * 删除下载数据
- * @param url
- * @param name
- */
- private void deleteDownloading(String url, String name) {
- if (true == finished) {
- // 删除保存的URL。这个listurl主要是为了在列表中按添加下载任务的顺序进行显示
- for (int i = 0; i < AppConstants.listUrl.size(); i++) {
- if (url.equalsIgnoreCase(AppConstants.listUrl.get(i))) {
- AppConstants.listUrl.remove(i);
- }
- }
- // 删除已经完成的下载任务
- if (AppConstants.mapTask.containsKey(url)) {
- AppConstants.mapTask.remove(url);
- }
- }
- SQLiteDatabase db = dbHelper.getWritableDatabase();
- String sql = "DELETE FROM fileDownloading WHERE downloadUrl=? AND name=?";
- db.execSQL(sql, new Object[] { url, name });
- db.close();
- }
- public void setDataMap(HashMap<String, String> dataMap) {
- this.dataMap = dataMap;
- }
- public HashMap<String, String> getDataMap() {
- return dataMap;
- }
- public boolean isPaused() {
- return paused;
- }
- public int getCurSize() {
- return curSize;
- }
- public int getLength() {
- return length;
- }
- public void setContext(Context context) {
- this.context = context;
- }
- public Context getContext() {
- return context;
- }
- public void setListView(ListView listView) {
- this.listView = listView;
- }
- }
好了,下载任务已经启动了,接下来就该开始管理了。先说说之前错误的思路,估计大多数的网友可能跟我一样,一想到列表首先想到的就是ListView,这多简单啊,放一个ListView,继承BaseAdapter写个自己的Adapter,然后一展现,完事了,so easy。我也是这么想的,这省事啊,用了以后才发现,确实省事,不过更新ProgressBar的时候可是给我愁死了,无论怎么着都不能正常更新ProgressBar。在这个地方钻了一周的牛角尖,昨儿个突然灵光乍现,干嘛给自己挖个坑,谁说列表就非得用ListView了,我自己写个列表不就得了。
先来看看列表页都有些什么
- package com.test.muldownloadtest;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.Window;
- import android.widget.Button;
- import android.widget.LinearLayout;
- import android.widget.ScrollView;
- public class DownloadManagerActivity extends Activity implements OnClickListener {
- private ScrollView scDownload;
- private LinearLayout llDownloadLayout;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- this.requestWindowFeature(Window.FEATURE_NO_TITLE);
- this.setContentView(R.layout.download_manager_layout);
- initView();
- }
- @Override
- protected void onResume() {
- super.onResume();
- refreshItemView();
- }
- private void initView(){
- Button btnGoback = (Button) this.findViewById(R.id.btnGoback);
- btnGoback.setOnClickListener(this);
- scDownload = (ScrollView) this.findViewById(R.id.svDownload);
- scDownload.setSmoothScrollingEnabled(true);
- llDownloadLayout = (LinearLayout) this.findViewById(R.id.llDownloadLyout);
- }
- /**
- * 列表中的每一项
- */
- private void refreshItemView(){
- for (int i = 0; i < AppConstants.listUrl.size(); i++) {
- DownloadItemView downloadItemView = new DownloadItemView(this, AppConstants.listUrl.get(i), i);
- downloadItemView.setId(i);
- downloadItemView.setTag("downloadItemView_"+i);
- llDownloadLayout.addView(downloadItemView);
- }
- }
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.btnExit:
- this.finish();
- break;
- case R.id.btnGoback:
- this.finish();
- break;
- default:
- break;
- }
- }
- }
很简单,一个ScrollView,在这个ScrollView中在内嵌一个LinearLayout,用这个LinearLayout来存储每一个列表项。其实列表项很简单,最基本只要三个控件就行了——ProgressBar、TextView、Button。一个是进度条,一个显示百分比,一个用来暂停/继续,偷个懒,这个布局文件就不列出来了,咱就看看这个Button都干嘛了。
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.btnPauseOrResume:
- String btnTag = (String) btnPauseOrResume.getTag();
- if (btnTag.equals("pause")) {
- resumeDownload();
- }
- else if (btnTag.equals("resume")) {
- pauseDownload();
- }
- break;
- default:
- break;
- }
- }
- private void pauseDownload(){
- btnPauseOrResume.setTag("pause");
- btnPauseOrResume.setText(R.string.download_resume);
- Async pauseTask = null;
- // 判断当前被停止的这个任务在任务列表中是否存在,如果存在就暂停
- if (AppConstants.linkedMapDownloading.containsKey(urlString)) {
- pauseTask = AppConstants.linkedMapDownloading.get(urlString);
- if (pauseTask != null) {
- pauseTask.pause();
- }
- }
- }
- private void resumeDownload(){
- btnPauseOrResume.setTag("resume");
- btnPauseOrResume.setText(R.string.download_pause);
- Async continueTask = null;
- // 判断当前被停止的这个任务在任务列表中是否存在,如果存在就继续
- if (AppConstants.linkedMapDownloading.containsKey(urlString)) {
- continueTask = AppConstants.linkedMapDownloading.get(urlString);
- if (continueTask != null) {
- continueTask.continued();
- }
- }
- handler.postDelayed(runnable, 1000);
- }
简单吧,就是判断一下当前按钮的Tag,然后根据Tag的值,来判断是继续下载,还是暂停下载。而这个暂停还是继续,其实只是修改下Async中的暂停标记的值,即paused是true还是false。
到此,核心功能展示完毕。附效果图一张
Demo下载地址 http://www.eoeandroid.com/forum.php?mod=viewthread&tid=230817&page=1&extra=#pid2067386