DownloadManager的使用方法:1

DownloadManager的使用方法:一

下载文件

 DownloadManager downloadManager = (DownloadManager) MainActivity.this.getSystemService(DOWNLOAD_SERVICE);
                Uri uri = Uri.parse("http://www.apk3.com/uploads/soft/201504/vnuuuxvobiu.apk");
                DownloadManager.Request request = new DownloadManager.Request(uri);               
                long download_id = downloadManager.enqueue(request);
                //返回值download_id是一个比较重要的参数,主要用来查询

以上步骤就是一个完整地比较简单的下载代码。

1.这个文件会被保存在系统文件下载的默认路径中

2.而且无论网络环境怎样(WIFI,MOBILE)都会执行下载

3.当手机没有网络连接时,下载任务还是会执行,但是会处于“待连接”状态,一旦手机网络连接畅通,自动开始下载任务。

4.下载过程中断开网络连接,也会处于“待连接”状态,网络畅通自动开始下载任务。

5.当路径中该文件已经存在时,会自动以迭代的方式命名。

6.同一个任务可以多次 重复,同时 进行下载。

7.如果下载的连接是无效的,Notification会一直停在 “待连接状态”,并且无法手动移除。

DownloadManager的使用方法:1

DownloadManager的使用方法:1

指定下载位置,及文件名称

request.setDestinationInExternalFilesDir(context,"TestDownload","Test.apk");

 request.setDestinationInExternalPublicDir("TestDownload","Test.apk");

这两个方法的后面两个参数,分别是 “文件路径”,“文件名称”

setDestinationInExternalFilesDir方法

//官方文档说明
Set the local destination for the downloaded file to a path within the application's external files directory (as returned by getExternalFilesDir(String).

这个方法是给下载文件 “制定” 一个路径,文件路径的“特性”跟 getExternalFilesDir(String)类似。

getExternalFilesDir(String)

这个方法的返回值 是一个文件夹,这个文件夹是被创建在 你的SD卡中的Android/data/data/your_package/。

他一般是用来存储你的app运行所需的文件的(例如,网上一些图片的缓存)。

默认情况下这个文件夹只有当前app有访问权限

当你的应用程序卸载之后 这个文件夹中的数据都会消失

setDestinationInExternalPublicDir方法

//官方说明
Set the local destination for the downloaded file to a path within the public external storage directory (as returned by getExternalStoragePublicDirectory(String)).

这个方法也是用来“制定”一个路径的,这个路径的特性类似于getExternalStoragePublicDirectory(String))

getExternalStoragePublicDirectory(String))

这个方法的返回值 是一个文件夹,这个文件夹是被创建在你的SD卡根目录的(mnt/sdcard/)

这个文件夹中的内容其他程序都是可以访问的(没有访问控制)

当你的应用程序卸载的时候,这个文件夹中的内容将不会丢失。

指定下载的网络类型

指定类型

//指定在WIFI状态下,执行下载操作。
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
//指定在MOBILE状态下,执行下载操作
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE);

是否允许漫游状态下,执行下载操作

request.setAllowedOverRoaming(boolean);方法来设置,是否同意漫游状态下 执行操作。 (true,允许; false 不允许;默认是允许的。)

是否允许“计量式的网络连接”执行下载操作

request.setAllowedOverMetered(boolean); 默认是允许的。

//根据你的网络流量 的消耗来收费的网络

Internet service providers can charge by the amount of data used (the amount of data sent and received by your PC). That's called a metered Internet connection. These plans often have a data limit, and if you exceed the limit you might have to pay extra. In some cases, you aren't charged extra but your connection speed becomes slower until the billing cycle ends.

If you have a metered Internet connection, setting your network connection to metered in Windows can help you reduce the amount of data you send and receive.

定制Notification样式

设置Notification的标题和描述

request.setTitle("标题");  
request.setDescription("描述"); 

设置Notification的显示,和隐藏。

request.setNotificationVisibility(visibility);

VISIBILTY_HIDDEN: Notification将不会显示,如果设置该属性的话,必须要添加权限android.permission.DOWNLOAD_WITHOUT_NOTIFICATION.

VISIBILITY_VISIBLE: Notification显示,但是只是在下载任务执行的过程中显示,下载完成自动消失。(默认值)

VISIBILITY_VISIBLE_NOTIFY_COMPLETED : Notification显示,下载进行时,和完成之后都会显示。

VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION :只有当任务完成时,Notification才会显示。

DownloadManger中的广播事件

该类中的广播会在特定的时候自动发送,我们只要定制自己的Receiver来接收广播就行.
广播 注册之后 记得在Activity销毁的时候 注销,避免内存泄露。

IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
receiver = new DownloadCompleteReceiver();
registerReceiver(receiver,filter);
class DownloadCompleteReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {

            long downloaded_id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID,-1);

           /* Log.d(getPackageName(),"下载完成:"+"downloaded_id ="+downloaded_id+"----" +"download_id="+download_id1+"");
            Log.d(getPackageName(),"下载完成:"+"downloaded_id ="+downloaded_id+"----" +"download_id="+download_id2+"");*/
            if(download_id1 == downloaded_id){
                Log.d(getPackageName(),"下载完成:"+"downloaded_id ="+downloaded_id);
            }
            if(download_id2 == downloaded_id){
                Log.d(getPackageName(),"下载完成:"+"downloaded_id ="+downloaded_id);
            }
        }

    }

下载完成时,发送的广播。

对应的Action为:ACTION_DOWNLOAD_COMPLETE

Notification被点击时发送的广播。

对应的Action为:ACTION_NOTIFICATION_CLICKED

查看所有下载情况的广播。

对应的Action为:ACTION_VIEW_DOWNLOADS

未完……