-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownloadUtils.java
More file actions
132 lines (118 loc) · 6.68 KB
/
DownloadUtils.java
File metadata and controls
132 lines (118 loc) · 6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.widget.Toast;
public class DownloadUtils {
private Context mContext;
private String downloadFileName = "weiyan.apk";
private static long myReference;
private static DownloadManager downloadManager;
private DownloadManager.Request downloadRequest;
/**
* DownloadUtils downloadUtils = new DownloadUtils(activity, downloadUrl);
* //下载显示名字,不能是中文
* downloadUtil.setDownloadFileName("apkName" + System.currentTimeMillis() + ".apk");
* downloadUtil.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
* downloadUtil.start();
*/
public DownloadUtils(Context context, String downloadUrl) {
this.mContext = context;
initDownload(downloadUrl);
}
private void initDownload(String downloadUrl) {
downloadManager = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse(downloadUrl);//"http://app.mi.com/download/25323"
downloadRequest = new DownloadManager.Request(uri);
// 设置目标存储在外部目录,一般位置可以用
downloadRequest.setDestinationInExternalFilesDir(mContext, Environment.DIRECTORY_DOWNLOADS, downloadFileName);
//下载的文件能被其他应用扫描到
downloadRequest.allowScanningByMediaScanner();
//设置被系统的Downloads应用扫描到并管理,默认true
downloadRequest.setVisibleInDownloadsUi(true);
//限定在WiFi还是手机网络(NETWORK_MOBILE)下进行下载
downloadRequest.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
// 设置mime类型,这里看服务器配置,一般国家化的都为utf-8编码。
downloadRequest.setMimeType("application/vnd.android.package-archive");
/**
* 设置notification显示状态
* Request.VISIBILITY_VISIBLE:在下载进行的过程中,通知栏中会一直显示该下载的Notification,当下载完成时,该Notification会被移除,这是默认的参数值。
* Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED:在下载过程中通知栏会一直显示该下载的Notification,在下载完成后该Notification会继续显示,直到用户点击该
* Notification或者消除该Notification。
* Request.VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION:只有在下载完成后该Notification才会被显示。
* Request.VISIBILITY_HIDDEN:不显示该下载请求的Notification。如果要使用这个参数,需要在应用的清单文件中加上android.permission.DOWNLOAD_WITHOUT_NOTIFICATION
*/
downloadRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
//设置notification的标题
downloadRequest.setTitle("");
//设置notification的描述
downloadRequest.setDescription("");
}
public void start() {
myReference = downloadManager.enqueue(downloadRequest);
}
/**
* 须static,不然在manife注册时报错:java.lang.InstantiationException has no zero argument constructor
* 或者must be registered and unregistered inside the Parent class
*/
public static class DownloadManagerReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//Notification点击
if (intent.getAction().equals(DownloadManager.ACTION_NOTIFICATION_CLICKED)) {
String extraID = DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS;
long[] references = intent.getLongArrayExtra(extraID);
for (long reference : references)
if (reference == myReference) {
}
}
//下载完成
if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
long completeDownloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
if (completeDownloadId == myReference) {
Cursor cursor = downloadManager.query(new DownloadManager.Query()
.setFilterById(completeDownloadId));
cursor.moveToFirst();
String filePath = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
cursor.close();
if (filePath != null) {
if (filePath.contains(context.getPackageName())) {
AppUtils.installAPK(context, filePath.trim().substring(7));
}
} else {
Toast.makeText(context, "网络不给力", Toast.LENGTH_SHORT).show();
}
}
}
}
}
public void setDownloadFileName(String downloadFileName) {
// 设置目标存储在外部目录,一般位置可以用
downloadRequest.setDestinationInExternalFilesDir(mContext, Environment.DIRECTORY_DOWNLOADS, downloadFileName);
}
public void setNotificationTitle(CharSequence title) {
//设置notification的标题
downloadRequest.setTitle(title);
}
public void setNotificationDescription(CharSequence description) {
//设置notification的描述
downloadRequest.setDescription(description);
}
/**
* 设置notification显示状态
* Request.VISIBILITY_VISIBLE:在下载进行的过程中,通知栏中会一直显示该下载的Notification,当下载完成时,该Notification会被移除,这是默认的参数值。
* Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED:在下载过程中通知栏会一直显示该下载的Notification,在下载完成后该Notification会继续显示,直到用户点击该
* Notification或者消除该Notification。
* Request.VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION:只有在下载完成后该Notification才会被显示。
* Request.VISIBILITY_HIDDEN:不显示该下载请求的Notification。如果要使用这个参数,需要在应用的清单文件中加上android.permission.DOWNLOAD_WITHOUT_NOTIFICATION
*/
public void setNotificationVisibility(int visibility) {
downloadRequest.setNotificationVisibility(visibility);
}
public DownloadManager.Request getDownloadRequest() {
return downloadRequest;
}
}