admin 管理员组文章数量: 887021
在项目中使用到了在android端下载服务端的文件,之前使用的是http下载方式,现在想改成调用系统的下载器进行下载,实现步骤为:
1.得到url:文件下载地址
2.使用URL下载:
(1)下载前先判断文件是否已经在下载,避免重复下载:
// 判断该文件是否在下载
private boolean isDowning(String uri) {
boolean flag = false;
try {
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterByStatus(DownloadManager.STATUS_RUNNING);
if (downloadManager == null) {
downloadManager = ((DownloadManager) this
.getSystemService(Activity.DOWNLOAD_SERVICE));
}
Cursor c = downloadManager.query(query);
String downingURI;
while (c.moveToNext()) {
downingURI = c.getString(c
.getColumnIndex(DownloadManager.COLUMN_URI));
if (downingURI.equalsIgnoreCase(uri)) {
flag = true;
break;
}
}
if (c != null) {
c.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
return flag;
}
(2)如果未在下载则下载文件:
// 本地存储路径
Request request = new Request(Uri.parse(url));
// 在通知栏中显示
request.setShowRunningNotification(true);
request.setVisibleInDownloadsUi(true);
String dir ="tempFilrDir";//本地文件存储目录 相对路径
String fileName = "悟空.mp3";//下载的文件名称
request.setDestinationInExternalPublicDir(dir, fileName);//文件存储路径 绝对路径
request.setTitle(<span style="font-family: KaiTi_GB2312;">fileName </span>);//下载时在通知栏显示的文字
//downloadManager = ((DownloadManager) this.getSystemService(Activity.DOWNLOAD_SERVICE));
<pre name="code" class="java">long downloadId = downloadManager.enqueue(request);//执行下载
DownloadCompleteReceiver.mDownloadIDs.add(new Long(downloadId));//
DownloadCompleteReceiver下载完成的接收器,第//3步会讲到
(3)接收完成的接收器
<span style="font-family: KaiTi_GB2312;"></span><pre name="code" class="java">package com.bcts.appmanage.manage;
import java.util.ArrayList;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import com.bcts.appmanage.tools.Constant;
/**
* 调用系统下载器下载文件
*
*/
public class DownloadCompleteReceiver extends BroadcastReceiver {
public static final String TAG = "DownloadCompleteReceiver";
public static ArrayList<Long> mDownloadIDs = new ArrayList<Long>();
public String fileName;
public Handler mHandler;
public DownloadCompleteReceiver(Handler handler) {
mHandler = handler ;
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
long downId = intent.getLongExtra(
DownloadManager.EXTRA_DOWNLOAD_ID, -1);
//
if (mDownloadIDs.contains(downId)) {
// //
DownloadManager downloadManager = (DownloadManager) context
.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Query query = new DownloadManager.Query();
// query.setFilterByStatus(DownloadManager.STATUS_SUCCESSFUL);
query.setFilterById(downId);
Cursor c = downloadManager.query(query);
if (c.moveToFirst()) {
fileName = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
Message m = new Message();
m.obj = fileName;
m.what = Constant.DOWNLOAD_FINISHED;
mHandler.sendMessage(m);
Log.i(TAG, "下载'" + fileName + "'完成");
}else{
Log.i(TAG, "下载'" + fileName + "'失败");
}
if (c != null) {
c.close();
}
mDownloadIDs.remove(downId);
}
}
}
}
(4)注册接收完成的广播
<span style="font-family: KaiTi_GB2312;"> </span><pre name="code" class="java"> downloadCompleteReceiver = new DownloadCompleteReceiver(mHandler);
IntentFilter myIntentFilter = new IntentFilter();
myIntentFilter.addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
// 注册广播
registerReceiver(downloadCompleteReceiver, myIntentFilter);
版权声明:本文标题:android调用系统下载器下载文件 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1731547160h1492090.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论