Here are the examples of the java api class android.app.DownloadManager.Query taken from open source projects.
1. DownloadCompleteReceiver#onReceive()
Project: HttpTest
File: DownloadCompleteReceiver.java
File: DownloadCompleteReceiver.java
@Override public void onReceive(Context context, Intent intent) { long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0); Handler mHandler = HandlerManager.getHandler(); mHandler.sendMessage(Message.obtain(mHandler, DownloadManagerActivity.MSG_WHAT_DOWNLOAD_ID, id)); if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) { Toast.makeText(context, "?? " + id + " ??????????", Toast.LENGTH_SHORT).show(); } else if (intent.getAction().equals(DownloadManager.ACTION_NOTIFICATION_CLICKED)) { Toast.makeText(context, "???-" + id, Toast.LENGTH_SHORT).show(); } else if (intent.getAction().equals(DownloadManager.ACTION_VIEW_DOWNLOADS)) { Toast.makeText(context, "?????-" + id, Toast.LENGTH_SHORT).show(); } //??????APK???????? DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); Query query = new DownloadManager.Query(); query.setFilterById(id); Cursor cursor = downloadManager.query(query); if (cursor != null) { if (cursor.moveToFirst()) { int status = cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS)); String name = cursor.getString(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_LOCAL_FILENAME)); String uri = cursor.getString(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_LOCAL_URI)); String end = name.substring(name.lastIndexOf(".")); if (end != null && end.equals(".apk") && status == DownloadManager.STATUS_SUCCESSFUL) { Intent mIntent = new Intent(Intent.ACTION_VIEW); mIntent.setDataAndType(Uri.parse(uri), "application/vnd.android.package-archive"); mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(mIntent); } } cursor.close(); } }
2. AttachmentDownloadReceiver#downloadCompleted()
Project: helpstack-android
File: AttachmentDownloadReceiver.java
File: AttachmentDownloadReceiver.java
private void downloadCompleted(Context context, Intent intent) { //Files are ready String filename = context.getString(R.string.hs_attachment); String filepath = null; String mediaType = null; DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0); StringBuilder text = new StringBuilder(); Query query = new Query(); query.setFilterById(downloadId); Cursor c = dm.query(query); if (c.moveToFirst()) { int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)); filename = c.getString(c.getColumnIndex(DownloadManager.COLUMN_TITLE)); filepath = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)); mediaType = c.getString(c.getColumnIndex(DownloadManager.COLUMN_MEDIA_TYPE)); if (status == DownloadManager.STATUS_SUCCESSFUL) { text.append(context.getString(R.string.hs_download_complete)); } else { text.append(context.getString(R.string.hs_error_during_download)); } } NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context); notificationBuilder.setAutoCancel(true); notificationBuilder.setContentText(text.toString()); notificationBuilder.setContentTitle(filename); notificationBuilder.setSmallIcon(R.drawable.hs_download_light); notificationBuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE); notificationBuilder.setContentIntent(getPendingIntent(context)); notificationManager.notify(filename, NOTIFICATION_ID, notificationBuilder.build()); }