public class MainActivity extends Activity {
private Button btn;
private String url="http://www.oschina.net/uploads/osc-android-v2.6.4-release.apk";
private String sdpath="/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);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
HttpUtils httpUtils=new HttpUtils();
httpUtils.download(url, sdpath, 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) {
// TODO Auto-generated method stub
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() {
// TODO Auto-generated method stub
super.onStart();
btn.setText("开始下载");
}
});
}
});
}
}
private Button btn;
private String url="http://www.oschina.net/uploads/osc-android-v2.6.4-release.apk";
private String sdpath="/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);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
HttpUtils httpUtils=new HttpUtils();
httpUtils.download(url, sdpath, 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) {
// TODO Auto-generated method stub
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() {
// TODO Auto-generated method stub
super.onStart();
btn.setText("开始下载");
}
});
}
});
}
}