使用以下代码段:
try {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse((url + "?sid=" + sessionId)));
String filename = itemId + ".pkpass";
File downloadDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setDescription(getString(R.string.item_description_download));
request.setTitle(filename);
request.setDestinationUri(Uri.fromFile(new File(downloadDir, filename)));
request.addRequestHeader("User-Agent", System.getProperty("http.agent") + " my_app/" + Utils.appVersionNumber());
downloadId = dManager.enqueue(request);
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterByStatus(DownloadManager.PAUSED_UNKNOWN);
Cursor c = dManager.query(query);
if (c.moveToFirst()) {
int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
if (status == DownloadManager.PAUSED_UNKNOWN) {
// Occurs when using Android API 16 to 19
}
}
} catch (Exception e) {
showToast(getString(R.string.item_download_error));
}
API 21(Android 5.0)+工作得很好。没问题。
现在,当我使用Android API 16(Android 4.1-4.1.1)到API 19时,它将无法下载文件,只能保持状态为PAUSED_UNKNOWN
的无限“下载循环”。
任何人都知道这个问题吗?
也允许权限......
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
...
摇篮:
compileSdkVersion 25
buildToolsVersion '25.0.3'
minSdkVersion 16
targetSdkVersion 21