setContentView(R.layout.main); BroadcastReceiver receiver = 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); Query query = new Query(); query.setFilterById(enqueue); Cursor c = dm.query(query); if (c.moveToFirst()) { int columnIndex = c .getColumnIndex(DownloadManager.COLUMN_STATUS); if (DownloadManager.STATUS_SUCCESSFUL == c .getInt(columnIndex)) { Request request = new Request( Uri.parse("url for file to download")); enqueue = dm.enqueue(request); Intent i = new Intent(); i.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS); startActivity(i);
public static int remove(Context context, long id) { DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); return dm.remove(id); }
DownloadManager mgr = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); long id = mgr.enqueue(request); DownloadManager.Query q = new DownloadManager.Query(); q.setFilterById(id); Cursor cursor = mgr.query(q); cursor.moveToFirst(); int bytes_downloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)); cursor.close();
request.setDescription(Main.this.getString(R.string.notification_description)); request.setTitle(Main.this.getString(R.string.app_name)); request.setDestinationUri(uri); final long downloadId = manager.enqueue(request); BroadcastReceiver onComplete = new BroadcastReceiver() { public void onReceive(Context ctxt, Intent intent) { Intent install = new Intent(Intent.ACTION_VIEW); install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); install.setDataAndType(uri, manager.getMimeTypeForDownloadedFile(downloadId)); startActivity(install);
@Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) { DownloadManager.Query query = new DownloadManager.Query(); query.setFilterById(intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0)); DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); Cursor cursor = manager.query(query); if (cursor.moveToFirst()) { if (cursor.getCount() > 0) { int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)); if (status == DownloadManager.STATUS_SUCCESSFUL) { String file = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME)); // So something here on success } else { int message = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_REASON)); // So something here on failed. } } } } }
@SuppressLint("NewApi") @Override public void onReceive(final Context context, final Intent intent) { final String action = intent.getAction(); if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) { final long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0); final DownloadManager.Query query = new DownloadManager.Query(); query.setFilterById(downloadId); final Cursor c = mDownloadManager.query(query); if (c.moveToFirst()) { final int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS); if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) { // we're not checking download id here, that is done in handleDownloads final String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)); handleDownloads(uriString); } else { final int reason = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_REASON)); Log.e(TAG, "Download failed: " + reason); } } } } };
DownloadManager downloadManager = (DownloadManager)context.getSystemService(Context.DOWNLOAD_SERVICE); long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0); DownloadManager.Query query = new DownloadManager.Query(); query.setFilterById(downloadId); Cursor c = downloadManager.query(query); if(c != null) { if (c.moveToFirst()) { int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS); if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) { String downloadFileUrl = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)); startInstall(context, Uri.parse(downloadFileUrl)); return false; Intent intent = new Intent(); intent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction( Intent.ACTION_VIEW);
URL kanboardURL = KanboardAPI.sanitizeURL(serverURL.trim()); String request = intent.getStringExtra("request"); String filename = intent.getStringExtra("filename"); if (BuildConfig.DEBUG) { Log.d(Constants.TAG, request); Intent stopIntent = new Intent(this, DownloadIntentService.StopServiceIntent.class); stopIntent.putExtra("serviceIntent", intent); dm.addCompletedDownload(file.getName(), getString(R.string.download_manager_name), false, mime, file.getPath(), file.length(), true); Thread.sleep(500); mNotificationManager.cancel(554);
/** * Return the intent to launch for a given download item. * * @param context Context of the app. * @param downloadId ID of the download item in DownloadManager. * @return the intent to launch for the given download item. */ private static Intent getLaunchIntentFromDownloadId(Context context, long downloadId) { assert !ThreadUtils.runningOnUiThread(); DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); Uri uri = manager.getUriForDownloadedFile(downloadId); if (uri == null) return null; Intent launchIntent = new Intent(Intent.ACTION_VIEW); launchIntent.setDataAndType(uri, manager.getMimeTypeForDownloadedFile(downloadId)); launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION); return launchIntent; }
try { DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); dm.addCompletedDownload(filename, "xDrip+ update version " + newversion, false, "application/vnd.android.package-archive", getDownloadFolder() + "/" + filename, FILE_SIZE, true); } catch (Exception e) { Log.e(TAG, "Download manager error: " + e); final Intent installapk = new Intent(Intent.ACTION_VIEW); installapk.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); installapk.setDataAndType(Uri.fromFile(dest_file), "application/vnd.android.package-archive"); startActivity(installapk); finish();
@Override public void onReceive(Context context, Intent intent) { DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); String action = intent.getAction(); // We only want to check if the download has completed if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) { DownloadManager.Query query = new DownloadManager.Query(); query.setFilterById(downloadId); // Check if file has been successfully downloaded Cursor c = downloadManager.query(query); if (c.moveToFirst()) { int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS); int status = c.getInt(columnIndex); switch (status) { // If everything is fine, call the abstract method and proceed case DownloadManager.STATUS_SUCCESSFUL: Uri uri = Uri.parse(c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))); onDownloadDone(uri); break; // Otherwise tell the user that the download failed default: Toast.makeText(context, R.string.download_file_error, Toast.LENGTH_LONG); break; } context.unregisterReceiver(this); } // Close any open resources c.close(); } }
private void queryStatus(View v) { Cursor c= mgr.query(new DownloadManager.Query().setFilterById(lastDownload)); c.moveToFirst(); + c.getLong(c.getColumnIndex(DownloadManager.COLUMN_ID))); Log.d(getClass().getName(), "COLUMN_BYTES_DOWNLOADED_SO_FAR: "
final long id = downloadManager.enqueue(request); BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) { Intent installIntent = new Intent(Intent.ACTION_VIEW); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { installIntent.setDataAndType(downloadManager.getUriForDownloadedFile(id), "application/vnd.android.package-archive"); } else { Cursor cursor = downloadManager.query(new DownloadManager.Query().setFilterById(id)); try { if (cursor != null && cursor.moveToFirst()) { int status = cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS)); String localUri = cursor.getString(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_LOCAL_URI)); if (status == DownloadManager.STATUS_SUCCESSFUL) { installIntent.setDataAndType(Uri.parse(localUri), "application/vnd.android.package-archive");
@Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) { if (mDownloadManager != null) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { Uri downloadedUri = mDownloadManager.getUriForDownloadedFile(mLastDownloadId); String mimeType = mDownloadManager.getMimeTypeForDownloadedFile(mLastDownloadId); new NotifyDownloadedTask().execute(downloadedUri.toString(), mimeType); } } } else if (DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(action)) { Intent notiIntent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS); notiIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(notiIntent); } } };
private void installApk(Context context, long downloadId) { DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); Intent intent = new Intent(Intent.ACTION_VIEW); Uri fileUri = downloadManager.getUriForDownloadedFile(downloadId); if (null != fileUri) { LogUtils.d(TAG, fileUri.toString()); intent.setDataAndType(fileUri, "application/vnd.android.package-archive"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } else { LogUtils.d(TAG, "æ— APK,下载失败"); } } }
@Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) { // get the DownloadManager instance DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); DownloadManager.Query q = new DownloadManager.Query(); Cursor c = manager.query(q); if(c.moveToFirst()) { do { String name = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME)); Log.i("DOWNLOAD LISTENER", "file name: " + name); } while (c.moveToNext()); } else { Log.i("DOWNLOAD LISTENER", "empty cursor :("); } c.close(); } }
@Override public void onReceive(Context context, Intent intent) { long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0L); if (id != mDownloadId) { Log.w(TAG, "Ignore unrelated download"); return; } Log.w(TAG, "Image download completed"); Bundle extras = intent.getExtras(); DownloadManager.Query q = new DownloadManager.Query(); q.setFilterById(extras.getLong(DownloadManager.EXTRA_DOWNLOAD_ID)); Cursor c = mDownloadManager.query(q); if (c.moveToFirst()) { int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)); if (status == DownloadManager.STATUS_SUCCESSFUL) { // process download String filePath = c.getString(c.getColumnIndex( DownloadManager.COLUMN_LOCAL_FILENAME)); showFloorPlanImage(filePath); } } c.close(); } };
/** * get string column * * @param downloadId * @param columnName * @return */ private String getString(long downloadId, String columnName) { DownloadManager.Query query = new DownloadManager.Query() .setFilterById(downloadId); String result = null; Cursor c = null; try { c = downloadManager.query(query); if (c != null && c.moveToFirst()) { result = c.getString(c.getColumnIndex(columnName)); } } finally { if (c != null) { c.close(); } } return result; }
private long enqueue; private DownloadManager dm; public void onClick(View view) { dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); Request request = new Request( Uri.parse("File URL")); enqueue = dm.enqueue(request); } public void showDownload(View view) { Intent i = new Intent(); i.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS); startActivity(i); }
Uri uri = Uri.parse(imageUrl); DownloadManager.Request request = new DownloadManager.Request(uri); request.setTitle(fileName); request.setDescription(fileName); request.setVisibleInDownloadsUi(true); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName); long ref = downloadManager.enqueue(request); final BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { long downloadReference = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1); Log.i("GenerateTurePDfAsync", "Download completed"); DownloadManager.Query query = new DownloadManager.Query(); query.setFilterById(downloadReference); Cursor cur = downloadManager.query(query); if (cur.moveToFirst()) { int columnIndex = cur.getColumnIndex(DownloadManager.COLUMN_STATUS); if (DownloadManager.STATUS_SUCCESSFUL == cur.getInt(columnIndex)) { String uriString = cur.getString(cur.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));