private Button btn;
// 通过解析数据获得的版本号
private String jiuurl = "http://www.oschina.net/uploads/osc-android-v2.6.4-release.apk";
// 下载电脑上的地址
private String xixurl = "/sdcard/" + System.currentTimeMillis() + "yuyin.apk";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
}
public void gengxin(View v) {
HttpUtils httpUtils = new HttpUtils();
httpUtils.download(jiuurl, xixurl, true, new RequestCallBack<File>() {
@Override
public void onSuccess(ResponseInfo<File> arg0) {
btn.setText("下载成功");
// 路径
String path = arg0.result.getPath();
File file = new File(path);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
startActivity(intent);
}
@Override
public void onFailure(HttpException arg0, String arg1) {
btn.setText("下载失败");
}
@Override
public void onLoading(long total, long current, boolean isUploading) {
super.onLoading(total, current, isUploading);
btn.setText("正在下载");
ProgressDialog dialog = new ProgressDialog(MainActivity.this);
dialog.setTitle("正在下载");
dialog.setMax((int) total);
dialog.setProgress((int) current);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
}
@Override
public void onStart() {
super.onStart();
btn.setText("开始下载");
}
});
}
// 通过解析数据获得的版本号
private String jiuurl = "http://www.oschina.net/uploads/osc-android-v2.6.4-release.apk";
// 下载电脑上的地址
private String xixurl = "/sdcard/" + System.currentTimeMillis() + "yuyin.apk";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
}
public void gengxin(View v) {
HttpUtils httpUtils = new HttpUtils();
httpUtils.download(jiuurl, xixurl, true, new RequestCallBack<File>() {
@Override
public void onSuccess(ResponseInfo<File> arg0) {
btn.setText("下载成功");
// 路径
String path = arg0.result.getPath();
File file = new File(path);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
startActivity(intent);
}
@Override
public void onFailure(HttpException arg0, String arg1) {
btn.setText("下载失败");
}
@Override
public void onLoading(long total, long current, boolean isUploading) {
super.onLoading(total, current, isUploading);
btn.setText("正在下载");
ProgressDialog dialog = new ProgressDialog(MainActivity.this);
dialog.setTitle("正在下载");
dialog.setMax((int) total);
dialog.setProgress((int) current);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
}
@Override
public void onStart() {
super.onStart();
btn.setText("开始下载");
}
});
}