android 斷點下載---XUtils

來源:互聯網
上載者:User

標籤:

配置許可權

<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();                    }                });    }}

 效果展示

 

android 斷點下載---XUtils

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.