ADM(下载管理器)中有一项功能,即如果用户触摸下载链接(不是网页),ADM(下载管理器)将作为能够下载文件的应用程序出现。
如果用户触摸下载链接,我应该怎么做,我的应用程序将出现一个能够下载文件的应用程序?
答案 0 :(得分:1)
DownloadData类
private long DownloadData (Uri uri, View v) {
long downloadReference;
// Create request for android download manager
downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(uri);
//Setting title of request
request.setTitle("Data Download");
//Setting description of request
request.setDescription("Android Data download using DownloadManager.");
//Set the local destination for the downloaded file to a path
//within the application's external files directory
if(v.getId() == R.id.DownloadMusic)
request.setDestinationInExternalFilesDir(MainActivity.this,
Environment.DIRECTORY_DOWNLOADS,"AndroidTutorialPoint.mp3");
else if(v.getId() == R.id.DownloadImage)
request.setDestinationInExternalFilesDir(MainActivity.this,
Environment.DIRECTORY_DOWNLOADS,"AndroidTutorialPoint.jpg");
//Enqueue download and save into referenceId
downloadReference = downloadManager.enqueue(request);
Button DownloadStatus = (Button) findViewById(R.id.DownloadStatus);
DownloadStatus.setEnabled(true);
Button CancelDownload = (Button) findViewById(R.id.CancelDownload);
CancelDownload.setEnabled(true);
return downloadReference;
}
上述代码说明:
downloadReference:这是一个唯一的ID,我们将参考具体的下载请求。
请求:将通过传递
通过getSystemService创建DownloadManager的实例DOWNLOAD_SERVICE。使用DownloadManager.Request(uri)在下一个语句中生成新请求。
setDestinationInExternalFilesDir:这将用于将文件保存在外部下载文件夹中。
downloadManager.enqueue(request):将与请求对应的新下载排入队列。下载管理器准备好执行并且连接可用后,下载将自动启动。
来源:https://www.codeproject.com/Articles/1112730/Android-Download-Manager-Tutorial-How-to-Download