Android DownloadManager

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功能介绍及使用示例

你可能感兴趣的:(download)