android直接调用系统的下载方法进行文件下载,不废话,直接上代码:
首先,创建系统下载的工具类,
DownloadManagerUtil.java:
import android.app.DownloadManager;
import android.content.Context;
import android.net.Uri;
import android.os.Environment;
public class DownloadManagerUtil {
private Context mContext;
public DownloadManagerUtil(Context context) {
mContext = context;
}
/**
*
* @param url 文件地址
* @param title 通知栏标题
* @param desc 通知栏描述
* @return
*/
public long download(String url, String title, String desc) {
Uri uri = Uri.parse(url);
DownloadManager.Request req = new DownloadManager.Request(uri);
//设置WIFI下进行更新
//req.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
//下载中和下载完后都显示通知栏
req.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
//使用系统默认的下载路径 此处为应用内 /android/data/packages ,所以兼容7.0
req.setDestinationInExternalFilesDir(mContext, Environment.DIRECTORY_DOWNLOADS, title);
//通知栏标题
req.setTitle(title);
//通知栏描述信息
req.setDescription(desc);
//设置类型为.apk
req.setMimeType("application/vnd.android.package-archive");
//获取下载任务ID
DownloadManager dm = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
return dm.enqueue(req);
}
/**
* 下载前先移除前一个任务,防止重复下载
*
* @param downloadId
*/
public void clearCurrentTask(long downloadId) {
DownloadManager dm = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
try {
dm.remove(downloadId);
} catch (IllegalArgumentException ex) {
ex.printStackTrace();
}
}
}
创建好工具类后,后面就是直接使用。代码如下:
首先,在调用下载的页面初始化工具类,然后调用下载,代码如下:
//创建全员变量
var downloadId = 0L
private var downloadManagerUtil: DownloadManagerUtil? = null
//在onCreate中初始化util
downloadManagerUtil = DownloadManagerUtil(this)
//启用下载任务
val url = "url"
val title = "下载中"
val desc = "下载完成后,点击安装"
//先移除上一个下载任务,防止重复下载
// 此处为单个文件下载,downloadId为下载任务id,可根据业务调整
if (downloadId != 0L) {
downloadManagerUtil!!.clearCurrentTask(downloadId)
}
downloadId = downloadManagerUtil!!.download(url, title, desc)
最后,需要建一个广播的接收类。用于接收下载完成的广播,代码如下:
DownloadReceiver.java:
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import com.blankj.utilcode.util.AppUtils;
import com.cdd.sale.utils.UriToPathUtil;
public class DownloadReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
installApk(context, id);
} else if (intent.getAction().equals(DownloadManager.ACTION_NOTIFICATION_CLICKED)) {
// DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
//获取所有下载任务Ids组
//long[] ids = intent.getLongArrayExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS);
//点击通知栏取消所有下载
//manager.remove(ids);
//Toast.makeText(context, "下载任务已取消", Toast.LENGTH_SHORT).show();
//处理 如果还未完成下载,用户点击Notification ,跳转到下载中心
Intent viewDownloadIntent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
viewDownloadIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(viewDownloadIntent);
}
}
private static void installApk(Context context, long downloadApkId) {
DownloadManager dManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
Intent install = new Intent(Intent.ACTION_VIEW);
Uri downloadFileUri = dManager.getUriForDownloadedFile(downloadApkId);
if (downloadFileUri != null) {
/* Log.d("DownloadManager", downloadFileUri.toString());
install.setDataAndType(downloadFileUri, "application/vnd.android.package-archive");
install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(install);*/
String url = UriToPathUtil.getFilePathFromUri(context, downloadFileUri);
AppUtils.installApp(url);
} else {
Log.e("DownloadManager", "download error");
}
}
}
注意在AndroidManifest中注册广播:
<receiver android:name="com.cdd.sale.util.DownloadReceiver">
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
<action android:name="android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED" />
</intent-filter>
</receiver>