我在一个生产应用中使用了DownloadManager,但现在在Android Pie(API 28)中失败了。请注意,该下载被标记为失败,并返回错误400。
@Override
public void onReceive(Context context, Intent intent) {
long reference = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(reference);
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
Cursor cursor = downloadManager.query(query);
cursor.moveToFirst();
int statusIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
int status = cursor.getInt(statusIndex);
if (status == DownloadManager.STATUS_SUCCESSFUL) {
int fileUriIndex = cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI);
String savedFileUri = cursor.getString(fileUriIndex);
ParcelFileDescriptor pfd = null;
try {
pfd = context.getContentResolver().openFileDescriptor(Uri.parse(savedFileUri), "r");
PamplonaParkingXMLParser pamplonaParkingXMLParser = new PamplonaParkingXMLParser();
ParkingList parkingList = pamplonaParkingXMLParser.parse(pfd);
ObservableObject.getInstance().updateValue(parkingList);
if (pfd != null) {
pfd.close();
}
} catch (XmlPullParserException e) {
Log.e(TAG, e.getDetail().getLocalizedMessage());
} catch (IOException | NullPointerException e) {
Log.e(TAG, e.getLocalizedMessage());
}
} else if(status == DownloadManager.STATUS_FAILED){
Log.e(TAG, "Download failed");
int reasonIndex = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
int reason = cursor.getInt(reasonIndex);
Log.e(TAG, reason +"");
}
}