DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setVisibleInDownloadsUi(true);
request.setTitle("下载");
request.setDescription("正在更新管理软件");
request.setDestinationInExternalFilesDir(context, Environment.DIRECTORY_DOWNLOADS, "upgrade.apk");
DownloadManager downManager = (DownloadManager)context.getSystemService(Context.DOWNLOAD_SERVICE);
long id= downManager.enqueue(request);
SharedPreferences sPreferences = context.getSharedPreferences("download_id.info", Context.MODE_PRIVATE);
sPreferences.edit().putLong("id", id).commit();
下载完成后,会产生一个广播消息。在Intent中,通过下载id来判断是哪个下载任务。
long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
Broadcast的Action如下
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
</intent-filter>