Android中DownloadManager实现文件下载实例详解
Android中DownloadManager实现文件下载
下载
创建下载链接
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
设置允许下载的网络环境
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
WIFI网络 : DownloadManager.Request.NETWORK_WIFI
移动网络 : DownloadManager.Request.NETWORK_MOBILE
Notification显示下载进度
// 在Notification显示下载进度
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
// 设置Title
request.setTitle("更新");
// 设置描述
request.setDescription("正在下载更新文件...");
设置保存路径
private static final String DIR = "AutoUpdate"; private static final String APK = "MyHome.apk"; private static final String PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + DIR + "/" + APK; request.setDestinationInExternalPublicDir(DIR, APK);
下载
下载会返回一个进程ID
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); long id = downloadManager.enqueue(request);
取消下载
通过ID可以需要下载
downloadManager.remove(id);
下载完成的监听
下载完成,系统会发出广播,通过注册广播监听者可以监听到下载完成
广播的Action为DownloadManager.ACTION_DOWNLOAD_COMPLETE
/** * Broadcast intent action sent by the download manager when the user clicks on a running * download, either from a system notification or from the downloads UI. */ @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) public final static String ACTION_NOTIFICATION_CLICKED = "android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED";
Code
下载
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
// WIFI状态下下载
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
// 设置通知栏
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
request.setTitle("更新");
request.setDescription("正在下载更新文件...");
// 存放路径
request.setDestinationInExternalPublicDir(DIR, APK);
// 开始下载
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
long id = downloadManager.enqueue(request);
广播接收者
注册
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.kongqingwei.downloadmanagerdemo">
<!-- 网络权限 -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MANAGE_USERS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver android:name=".AutoUpdateBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
</intent-filter>
</receiver>
</application>
</manifest>
实现
package com.example.kongqingwei.downloadmanagerdemo;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
/**
* Created by kongqingwei on 2016/12/19.
* 广播接收者
*/
public class AutoUpdateBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) {
Toast.makeText(context, "下载完成", Toast.LENGTH_SHORT).show();
boolean isInstalled = AutoUpdater.installApk();
Toast.makeText(context, isInstalled ? "安装成功" : "安装失败", Toast.LENGTH_SHORT).show();
}
}
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
相关文章
Android中okhttp3.4.1+retrofit2.1.0实现离线缓存
这篇文章主要介绍了Android中okhttp3.4.1结合retrofit2.1.0实现离线缓存,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2016-10-10
Android通过ViewModel保存数据实现多页面的数据共享功能
这篇文章主要介绍了Android通过ViewModel保存数据实现多页面的数据共享功能,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下2019-11-11
浅析KJFrameForAndroid框架如何高效加载Bitmap
Bitmap是Android系统中的图像处理的最重要类之一。用它可以获取图像文件信息,进行图像剪切、旋转、缩放等操作,并可以指定格式保存图像文件。本文主要是从KJFrameForAndroid框架中分析高效加载Bitmap的方法2014-07-07


最新评论