install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); install.setDataAndType(uri, manager.getMimeTypeForDownloadedFile(downloadId)); startActivity(install);
/** * 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; }
@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); } } };
.getMimeTypeForDownloadedFile(id))) { return;
/** * Called to open a given download item that is downloaded by the android DownloadManager. * @param context Context of the receiver. * @param intent Intent from the android DownloadManager. */ private void openDownload(final Context context, Intent intent) { long ids[] = intent.getLongArrayExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS); if (ids == null || ids.length == 0) { DownloadManagerService.openDownloadsPage(context); return; } long id = ids[0]; DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); Uri uri = manager.getUriForDownloadedFile(id); if (uri == null) { // Open the downloads page DownloadManagerService.openDownloadsPage(context); } else { Intent launchIntent = new Intent(Intent.ACTION_VIEW); launchIntent.setDataAndType(uri, manager.getMimeTypeForDownloadedFile(id)); launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); try { context.startActivity(launchIntent); } catch (ActivityNotFoundException e) { DownloadManagerService.openDownloadsPage(context); } } }
mDownloadManager.getMimeTypeForDownloadedFile(mDownloadedFileID); fileIntent.setDataAndType(mostRecentDownload, mimeType); fileIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);