DownloadManager:系统的文件下载器

效果图

系统通知栏

DownloadManager:系统的文件下载器_第1张图片

系统文件夹

DownloadManager:系统的文件下载器_第2张图片

示例代码

String uriStr = "https://qd.myapp.com/myapp/qqteam/AndroidQQ/mobileqq_android.apk";
Uri mTargetUri = Uri.parse(uriStr);
DownloadManager.Request mRequest = new DownloadManager.Request(mTargetUri);
//设置允许使用的网络类型,这里是移动网络和wifi都可以
mRequest.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
//下载过程中通知栏会一直显示该下载,在下载完成后仍然会显示,直到用户点击该通知或者消除该通知
mRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
mRequest.setTitle("下载");
mRequest.setDescription("资源包正在下载");
//设置下载后文件存放的位置,后面两个参数分别是 “文件路径”,“文件名称”
mRequest.setDestinationInExternalFilesDir(this, Environment.DIRECTORY_DOWNLOADS, "meitu.apk");

DownloadManager mManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
if(mManager!=null){
   mManager.enqueue(mRequest);
}

DownloadManager类中的广播会在特定的时候自动发送,我们只要定制自己的Receiver来接收广播就行.

//下载完成时,发送广播对应的Action为:ACTION_DOWNLOAD_COMPLETE
//Notification被点击时发送广播对应的Action为:ACTION_NOTIFICATION_CLICKED
IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
DownloadCompleteReceiver receiver = new DownloadCompleteReceiver();
registerReceiver(receiver, filter);

class DownloadCompleteReceiver extends BroadcastReceiver {
     @Override
     public void onReceive(Context context, Intent intent) {

     }
}

Tip:可用FileObserver监听进度

你可能感兴趣的:(manager,下载,download)