zoukankan      html  css  js  c++  java
  • 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功能介绍及使用示例

  • 相关阅读:
    代码解析&Filter用户授权例子
    session
    软件工程结对作业2
    软件工程结对作业1
    软件工程第三次作业
    软件工程第二次作业
    软件工程第一次作业
    KMP算法
    哈希表
    Mysql事物隔离级别
  • 原文地址:https://www.cnblogs.com/ncore/p/4324195.html
Copyright © 2011-2022 走看看