package com.example.xutildownload;
import java.io.File;
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;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.text.format.Formatter;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends Activity {
private static final String URL = "http://ruishua-10007319.file.myqcloud.com/RsApps/ruishua.apk";
// 用于显示 当前已下载/总大小
private TextView mTv;
// 显示当前下载进度
private ProgressBar mPb;
// 使用xutil下载工具类
private HttpUtils mHttp;
//文件总大小
private long mTotal = 0;
//下载方法返回值
private HttpHandler<File> mHttpHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 创建下载工具类对象
mHttp = new HttpUtils();
// 初始化view
initView();
// 添加事件
initEvent();
//自动下载
startDowload();
}
private void startDowload() {
mHttpHandler = mHttp.download(URL, "/mnt/sdcard/test.apk", true, true, new RequestCallBack<File>() {
@Override
public void onLoading(long total, long current, boolean isUploading) {
// 下载过程中
mTotal = total;
String totalSize = Formatter.formatFileSize(MainActivity.this, total);
String currentSize = Formatter.formatFileSize(MainActivity.this, current);
// 显示当前已下载/总大小
mTv.setText(currentSize + "/" + totalSize);
// 计算百分比
int percent = (int) (current * 100.0 / total);
// 显示进度
mPb.setProgress(percent);
}
@Override
public void onSuccess(ResponseInfo<File> responseInfo) {
// 处理完成
File file = responseInfo.result;
long length = file.length();
Log.e("tag", "length=" + length +",mTotal=" + mTotal);
//显示下载完成
if (length >= mTotal) {
mTv.setText("下载完成");
}
}
@Override
public void onFailure(HttpException error, String msg) {
// 失败
mTv.setText("下载失败:" + msg);
}
});
}
/**
* 事件监听
*/
private void initEvent() {
mTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mHttpHandler.isCancelled()) {
//如果是暂停状态,那么应该是继续下载
startDowload();
}else{
//如果是正在下载,那么应该是暂停下载
mHttpHandler.cancel();
}
}
});
}
/**
* 初始化view
*/
private void initView() {
mTv = (TextView) findViewById(R.id.tv);
mPb = (ProgressBar) findViewById(R.id.pb);
}
}
//布局文件
<pre name="code" class="java"><RelativeLayout 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"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
<ProgressBar
android:id="@+id/pb"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:layout_below="@+id/tv" />
</RelativeLayout>
添加Xutils.jar包