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

  • 相关阅读:
    一个实现编译次数记录的jsfl
    特殊的RSS图标设置,您可以免费使用
    jquery实现的视差滚动教程(视差大背景效果)
    40个免费的wordpress主题推荐
    php+mysql方便的查询
    jQuery 简单实现select二级联动
    我对Oracle的刷未提交数据到文件的学习体会
    dbms_output.put_line的小例子
    isqlplus 的 define 与 pl/sql 的 &
    PLSQL 的 for循环的小例子
  • 原文地址:https://www.cnblogs.com/ncore/p/4324195.html
Copyright © 2011-2022 走看看