下面列出了android.app.DownloadManager#ACTION_VIEW_DOWNLOADS 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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);
}
}
@Override
public void onReceive(Context context, Intent intent) {
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) {
long downloadApkId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
long localDownloadId = UpdaterUtils.getLocalDownloadId(context);
if (downloadApkId == localDownloadId) {
Logger.get().d("download complete. downloadId is " + downloadApkId);
installApk(context, downloadApkId);
}
} else if (DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(intent.getAction())) {
//处理 如果还未完成下载,用户点击Notification
Intent viewDownloadIntent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
viewDownloadIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(viewDownloadIntent);
}
}
/**
* Open the Activity which shows a list of all downloads.
* @param context Application context
*/
protected static void openDownloadsPage(Context context) {
Intent pageView = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
pageView.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
context.startActivity(pageView);
} catch (ActivityNotFoundException e) {
Log.e(TAG, "Cannot find Downloads app", e);
}
}
/**
* Open the download manager app. If no app is found that can handle
* this intent, a a system chooser is shown.
*
* @param context
*/
public static void openDownloadManager(Context context) {
Intent downloadManagerIntent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
downloadManagerIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
context.startActivity(downloadManagerIntent);
} catch (ActivityNotFoundException e) {
context.startActivity(Intent.createChooser(downloadManagerIntent, context.getString(R.string.action_open)));
}
}
/**
* Open the Activity which shows a list of all downloads.
* @param context Application context
*/
protected static void openDownloadsPage(Context context) {
if (DownloadUtils.showDownloadManager(null, null)) return;
// Open the Android Download Manager.
Intent pageView = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
pageView.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
context.startActivity(pageView);
} catch (ActivityNotFoundException e) {
Log.e(TAG, "Cannot find Downloads app", e);
}
}
/**
* Open the Activity which shows a list of all downloads.
* @param context Application context
*/
public static void openDownloadsPage(Context context) {
if (DownloadUtils.showDownloadManager(null, null)) return;
// Open the Android Download Manager.
Intent pageView = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
pageView.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
context.startActivity(pageView);
} catch (ActivityNotFoundException e) {
Log.e(TAG, "Cannot find Downloads app", e);
}
}
@TargetApi(VERSION_CODES.O)
private static void copyToDownload(
Context context, Uri sourceUri, String fileName, File destination) {
try {
if (AndroidVersionUtils.isApiLevelAtLeastOreo()) {
Files.copy(
context.getContentResolver().openInputStream(sourceUri),
destination.toPath(),
StandardCopyOption.REPLACE_EXISTING);
} else {
UpdateExperimentFragment.copyUriToFile(context, sourceUri, destination);
// Inform DownloadManager of completed download so the file shows in Downloads app
DownloadManager downloadManager =
(DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
downloadManager.addCompletedDownload(
destination.getName(),
destination.getName(),
true,
getMimeTypeFromFileName(fileName),
destination.getAbsolutePath(),
destination.length(),
false);
}
Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
PendingIntent pendingIntent =
PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_ONE_SHOT);
((NotificationManager)
context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE))
.notify(
NotificationIds.SAVED_TO_DEVICE,
new NotificationCompat.Builder(
context.getApplicationContext(), NotificationChannels.SAVE_TO_DEVICE_CHANNEL)
.setContentTitle(fileName)
.setContentText(context.getString(R.string.saved_to_device_text))
.setSubText(context.getString(R.string.save_to_device_channel_description))
.setSmallIcon(R.drawable.ic_notification_24dp)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.build());
} catch (IOException ioe) {
AppSingleton.getInstance(context)
.onNextActivity()
.subscribe(
activity -> {
AccessibilityUtils.makeSnackbar(
activity.findViewById(android.R.id.content),
context.getResources().getString(R.string.saved_to_device_error),
Snackbar.LENGTH_SHORT)
.show();
});
ioe.printStackTrace();
if (destination.exists()) {
destination.delete();
}
} finally {
AppSingleton.getInstance(context).setExportServiceBusy(false);
}
}