DownloadManager seems to be the right choice for an application with lots of background downloads on a broken mobile internet connection.
Using the tutorial code found on the Internet, the application can request a download from the DM system as follows:
// in onCreate() dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); // in requestData() Uri u = Uri.parse("http://server:8000/feed/data"); Request dreq = new Request(u); dreq.setNotificationVisibility(Request.VISIBILITY_HIDDEN); downloadID = dm.enqueue(dreq);
The URL in this code is a test server on the local computer. The URL works, the browser in the Android emulator can receive the page, and the server returns an HTTP 200 code when my application requests this page through the DownloadManager and the code above.
This is the corresponding code in ACTION_DOWNLOAD_COMPLETE BroadcastReceiver, which is called when the DM retrieves the file.
Query q = new Query(); q.setFilterById(downloadID); Log.i("handleData()", "Handling data"); Cursor c = dm.query(q); if (c.moveToFirst()) { Log.i("handleData()", "Download ID: " + downloadID + " / " + c.getInt(c.getColumnIndex(DownloadManager.COLUMN_ID))); Log.i("handleData()", "Download Status: " + c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS))); if (c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) { String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)); Log.i("handleData()", "Download URI: " + uriString); } else if (c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_FAILED) { Log.i("handleData()", "Reason: " + c.getString(c.getColumnIndex(DownloadManager.COLUMN_REASON))); } }
Strange result:
DOWNLOAD_STATUS - 16 (or STATUS_FAILED), but the reason is "placeholder".
Why? Why does this happen when the server returns a status code of 200? And why are there no reasons given by DownloadManager?
hez
source share