Skip to content

Instantly share code, notes, and snippets.

@chan-lee
Created August 26, 2014 09:05
Show Gist options
  • Save chan-lee/3b7e7ca17c6380147b5d to your computer and use it in GitHub Desktop.
Save chan-lee/3b7e7ca17c6380147b5d to your computer and use it in GitHub Desktop.
@Override
public void onResume(){
super.onResume();
IntentFilter completeFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
registerReceiver(completeReceiver, completeFilter);
}
@Override
public void onPause(){
super.onPause();
unregisterReceiver(completeReceiver);
}
private final BroadcastReceiver completeReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
if (downloadId == mDownloadingId) {
Query query = new Query();
query.setFilterById(mDownloadingId);
Cursor c = mDownloadManager.query(query);
if (c.moveToFirst()) {
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
//String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
mDownloadingId = 0;
mDownloadManager = null;
mDownloaded = true;
Toast.makeText(context, "download complete" ,Toast.LENGTH_SHORT).show();
}
}
}
}
}
};
private void startDownload(Uri uri) {
mDownloadManager = (DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setTitle("title");
request.setDescription("description");
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DCIM, "path");
mDownloadingId = mDownloadManager.enqueue(request);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment