使用xUtils框架实现下载断点续传

package com.example.kuangjiadownload;

import java.io.File;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    private String path = "http://192.168.21.1:8080/ok/TGPSetup.exe";
    private TextView tv;
    private ProgressBar pro;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.tv);
        pro = (ProgressBar) findViewById(R.id.pro);
        findViewById(R.id.btn).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                HttpUtils httpUtils = new HttpUtils();
                //要下载的地址,文件保存路径,支持断点续传,支持重命名,回调方法
                httpUtils.download(path, "sdcard/TGPSetup.exe", true, true,
                        new RequestCallBack<File>() {
                            
                            @Override
                            public void onSuccess(ResponseInfo<File> arg0) {
                                Toast.makeText(MainActivity.this,"ok", 0).show();
                                
                            }
                            
                            @Override
                            public void onFailure(HttpException arg0, String arg1) {
                                Toast.makeText(MainActivity.this,"ok", 0).show();
                                
                            }
                            @Override
                            public void onLoading(long total, long current,
                                    boolean isUploading) {
                                // TODO Auto-generated method stub
                                super.onLoading(total, current, isUploading);
                                pro.setMax((int)total);
                                pro.setProgress((int)current);
                                tv.setText(current*100/total+"%");
                                
                            }
                        });
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击下载" />
    <ProgressBar 
        android:id="@+id/pro"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@android:style/Widget.ProgressBar.Horizontal"
        
        />
    <TextView 
        android:id="@+id/tv"
          android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

</LinearLayout>
原文地址:https://www.cnblogs.com/84126858jmz/p/4966841.html