@Override public void onReceive(final Context context, final Intent intent) { long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -2); if (enqueue != -1 && id != -2 && id == enqueue) { Query query = new Query(); query.setFilterById(id); DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); Cursor c = dm.query(query); if (c.moveToFirst()) { int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS); int status = c.getInt(columnIndex); if (status == DownloadManager.STATUS_SUCCESSFUL) { if (Utils.checkMD5(matchedMd5, new File(dir, apkFilename))) { NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder .setSmallIcon(R.drawable.ic_stat_ytd) .setContentTitle(getString(R.string.title_activity_share)) .setContentText( "v" + matchedVersion + " " + getString(R.string.new_v_install)); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Intent intent1 = new Intent(); intent1.setAction(android.content.Intent.ACTION_VIEW); intent1.setDataAndType(fileUri, "application/vnd.android.package-archive"); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent1, 0); builder.setContentIntent(contentIntent); notificationManager.notify(2, builder.build()); } else { deleteBadDownload(context, intent); } } } } stopSelf(); }
private void onReceivedDownloadNotification(Context context, Intent intent) { if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) { long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1); DownloadItem item = Controller.getInstance().getDownloadItemById(id); if (item != null) { // This is one of our downloads. final DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); Query query = new Query(); query.setFilterById(id); Cursor cursor = downloadManager.query(query); if (cursor.moveToFirst()) { int localUriIndex = cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI); int reasonIndex = cursor.getColumnIndex(DownloadManager.COLUMN_REASON); int statusIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS); int status = cursor.getInt(statusIndex); if (status == DownloadManager.STATUS_SUCCESSFUL) { String localUri = cursor.getString(localUriIndex); Toast.makeText( context, String.format(getString(R.string.DownloadComplete), localUri), Toast.LENGTH_SHORT) .show(); Controller.getInstance().getDownloadsList().remove(item); showNotification( getString(R.string.DownloadComplete), item.getFileName(), getString(R.string.DownloadComplete)); } else if (status == DownloadManager.STATUS_FAILED) { int reason = cursor.getInt(reasonIndex); String message; switch (reason) { case DownloadManager.ERROR_FILE_ERROR: case DownloadManager.ERROR_DEVICE_NOT_FOUND: case DownloadManager.ERROR_INSUFFICIENT_SPACE: message = getString(R.string.DownloadErrorDisk); break; case DownloadManager.ERROR_HTTP_DATA_ERROR: case DownloadManager.ERROR_UNHANDLED_HTTP_CODE: message = getString(R.string.DownloadErrorHttp); break; case DownloadManager.ERROR_TOO_MANY_REDIRECTS: message = getString(R.string.DownloadErrorRedirection); break; default: message = getString(R.string.DownloadErrorUnknown); break; } Toast.makeText( context, String.format(getString(R.string.DownloadFailedWithErrorMessage), message), Toast.LENGTH_SHORT) .show(); Controller.getInstance().getDownloadsList().remove(item); } } } } else if (DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(intent.getAction())) { Intent i = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS); startActivity(i); } }