一。downloadmanager类说明:
从Android 2.3开始新增了一个下载管理类,在SDK的文档中我们查找android.app.DownloadManager可以看到。下载管理类可以长期处理多个HTTP下载任务,客户端只需要给出请求的Uri和存放目标文件的位置即可,下载管理使用了一个AIDL服务器所以可以放心的在后台执行,同时实例化的方法需要使用getSystemService(Context.DOWNLOAD_SERVICE) ,Android123再次提醒使用API Level为9的用户可以轻松的通过新增的这个API实现Android平台上的文件下载操作。
DownloadManager类提供了以下几种方法来处理,
二。用downloadManager 下载:
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype, long contentLength) {
AppUtils.LogD(mimetype);
// download file
DownloadManager downloadManager = ((DownloadManager) activity
.getSystemService(Activity.DOWNLOAD_SERVICE));
Request request = new Request(Uri.parse(url));
// set request header, 如session等
request.addRequestHeader("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
request.addRequestHeader("Accept-Language", "en-us,en;q=0.5");
request.addRequestHeader("Accept-Encoding", "gzip, deflate");
request.addRequestHeader("Accept-Charset",
"ISO-8859-1,utf-8;q=0.7,*;q=0.7");
request.addRequestHeader("Cache-Control", "max-age=0");
String host = "";
try {
host = new URL(url).getHost();
} catch (MalformedURLException e) {
e.printStackTrace();
}
String cookieStr = CookieManager.getInstance().getCookie(host);
if (!AppUtils.isEmpty(cookieStr)) {
request.addRequestHeader("Cookie", cookieStr + "; AcSe=0");
}
// generate filename dynamically
String fileName = contentDisposition.replaceFirst(
"attachment; filename=", "");
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS, fileName);
downloadManager.enqueue(request);
}
三。判断download finished:
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
long downloadId = intent.getLongExtra(
DownloadManager.EXTRA_DOWNLOAD_ID, 0);
Query query = new Query();
query.setFilterById(enqueue);
Cursor c = dm.query(query);
if (c.moveToFirst()) {
int columnIndex = c
.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == c
.getInt(columnIndex)) {
ImageView view = (ImageView) findViewById(R.id.imageView1);
String uriString = c
.getString(c
.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
view.setImageURI(Uri.parse(uriString));
}
}
}
}
};
registerReceiver(receiver, new IntentFilter(
DownloadManager.ACTION_DOWNLOAD_COMPLETE));
四。获取系统download history list:
Intent i = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
startActivity(i);
五。 获得刚下载的文件 和 打开该文件:
protected String getDownloadedFileName() {
String fileName = "";
DownloadManager downloadManager = (DownloadManager) activity
.getSystemService(Activity.DOWNLOAD_SERVICE);
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterByStatus(DownloadManager.STATUS_SUCCESSFUL);
Cursor c = downloadManager.query(query);
if (c.moveToFirst()) {
fileName = c.getString(c
.getColumnIndex(DownloadManager.COLUMN_TITLE));
}
return fileName;
}
public void openFileFromSDCard(String fileStr, String mimeType) {
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(fileStr);
intent.setDataAndType(Uri.fromFile(file), mimeType);
activity.startActivity(intent);
}