• android 断点下载---XUtils


    配置权限

    <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    

      /MainActivity.java

    package com.bawei.myxutils;
    
    import java.io.File;
    import java.math.BigDecimal;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Environment;
    import android.widget.ProgressBar;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import com.lidroid.xutils.HttpUtils;
    import com.lidroid.xutils.exception.HttpException;
    import com.lidroid.xutils.http.HttpHandler;
    import com.lidroid.xutils.http.ResponseInfo;
    import com.lidroid.xutils.http.callback.RequestCallBack;
    
    public class MainActivity extends Activity {
    
        private ProgressBar pb;
        private TextView text1;
        private TextView text2;
        private String path;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            pb = (ProgressBar) findViewById(R.id.progressBar1);// 进度条
            text1 = (TextView) findViewById(R.id.text1);// 显示当前下载了多少
            text2 = (TextView) findViewById(R.id.text2);// 显示下载总大小
            path = Environment.getExternalStorageDirectory().getPath();// 得到SD卡路径
            HttpUtils http = new HttpUtils();
            /**
             * url 下载的路径 target 下载文件保存的路径 autoResume 是否支持断点续传
             */
            HttpHandler handler = http.download(
                    "http://101.200.142.201:8080/tqyb/dancer.avi", path
                            + "/dancer.avi", true /* 如果目标文件存在,接着未完成的部分继续下载。 */,
                    true/* 如果从请求返回信息中获取到文件名,下载完成后自动重命名。 */,
                    new RequestCallBack<File>() {
                        @Override
                        public void onStart() {
                            // TODO Auto-generated method stub
                            super.onStart();
                            Toast.makeText(MainActivity.this, "开始下载", 0).show();
                        }
    
                        /**
                         * 加载进度 pb为进度条,实现进度条进度加载 total 总得大小 current 当前的大小
                         */
                        @Override
                        public void onLoading(long total, long current,
                                boolean isUploading) {
                            // TODO Auto-generated method stub
                            pb.setMax((int) total);
                            pb.setProgress((int) current);
                            // 将字节转换成M
                            BigDecimal filesize = new BigDecimal(current);
                            BigDecimal megabyte = new BigDecimal(1024 * 1024);
                            float returnValue = filesize.divide(megabyte, 2,
                                    BigDecimal.ROUND_UP).floatValue();
                            text1.setText(returnValue + "M");
                            // 将字节转换成M
                            BigDecimal filesize1 = new BigDecimal(total);
                            BigDecimal megabyte1 = new BigDecimal(1024 * 1024);
                            float returnValue1 = filesize1.divide(megabyte1, 2,
                                    BigDecimal.ROUND_UP).floatValue();
                            text2.setText(returnValue1 + "M");
    
                        }
    
                        @Override
                        public void onSuccess(ResponseInfo<File> arg0) {
                            // TODO Auto-generated method stub
                            Toast.makeText(MainActivity.this, "下载成功", 0).show();
                        }
    
                        @Override
                        public void onFailure(HttpException arg0, String arg1) {
                            // TODO Auto-generated method stub
                            Toast.makeText(MainActivity.this, "下载失败", 0).show();
                        }
                    });
    
        }
    
    }
    

     效果展示

     

  • 相关阅读:
    分布式跟踪工具pinpoint
    python调用阿里云产品接口实现自动发现异常访问ip并禁用2小时
    centos病毒
    Google Earth API开发者指南
    在vs中使用ZedGraph控件的一些记录
    A flexible charting library for .NET
    ZedGraph.dll
    WPF 动态模拟CPU 使用率曲线图
    C#调用GoogleEarth COM API开发
    使用WeifenLuo.WinFormsUI.Docking界面布局中的保存配置
  • 原文地址:https://www.cnblogs.com/jsonfan/p/5432821.html
Copyright © 2020-2023  润新知