码迷,mamicode.com
首页 > 移动开发 > 详细

Android DownloadManager

时间:2015-03-09 18:58:16      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

import android.app.DownloadManager;
import android.net.Uri;

import java.io.File;
import java.util.*;


public class DownloadActivity extends Activity {

DownloadCompleteReceiver completeReceiver;

onCreate:
        completeReceiver = new DownloadCompleteReceiver();
        /** register download success broadcast **/
        registerReceiver(completeReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

OnClick:
                //创建保存文件目录
                String path = Environment.getExternalStorageDirectory().getPath().concat("/com.buzz.exhibition/image/");
                File folder = new File(path);
                if (!folder.exists() || !folder.isDirectory()) {
                    folder.mkdirs();
                }


                DownloadManager manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                List<String> fileNameList = new ArrayList<String>();
                fileNameList.add("1.png");
                fileNameList.add("2.png");
                fileNameList.add("3.png");

                for (String fileName : fileNameList) {
                    String fileUrl = String.format("http://192.168.0.106:8080/exhibition/image/%s", fileName);
                    Log.i(TAG, fileUrl);
                    Log.i(TAG, fileName);
                    //创建下载请求
                    DownloadManager.Request down = new DownloadManager.Request(Uri.parse(fileUrl));
                    //设置允许使用的网络类型,这里是移动网络和wifi都可以
                    down.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
                    //后台下载
                    down.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
                    //显示下载界面
                    down.setVisibleInDownloadsUi(true);
                    //设置下载后文件存放的位置
                    down.setDestinationInExternalPublicDir("/com.buzz.exhibition/image/", fileName);
                    //将下载请求放入队列
                    manager.enqueue(down);
                }


 @Override
    protected void onDestroy() {
        super.onDestroy();
        if (completeReceiver != null) unregisterReceiver(completeReceiver);
    }


@Override
    protected void onResume() {
        registerReceiver(completeReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
        super.onResume();
    }

    //接受下载完成后的intent
    class DownloadCompleteReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
                long downId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
                Toast.makeText(context, intent.getAction() + "id : " + downId, Toast.LENGTH_SHORT).show();
            }
        }
    }

}

Ref:Android 演示 DownloadManager—Android 下载 apk 包并安装

Ref:Android系统下载管理DownloadManager功能介绍及使用示例

Android DownloadManager

标签:

原文地址:http://www.cnblogs.com/ncore/p/4324195.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!