builder = new AlertDialog.Builder(
MainActivity.this);
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
mDialogView = inflater.inflate(R.layout.progress_dialog_layout, null);
mNumberProgressBar = (NumberProgressBar)mDialogView.findViewById(R.id.number_progress_bar);
builder.setView(mDialogView);
mDialog = builder.create();
findViewById(R.id.circle_btn).setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
dismissDialog();
mNumberProgressBar.setProgress(0);
myTask = new MyAsyncTask();
myTask.execute(qqDownloadUrl);
}
});
private class DownloadAsyncTask extends AsyncTask<String , Integer, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mDialog.show();
}
@Override
protected void onPostExecute(String aVoid) {
super.onPostExecute(aVoid);
dismissDialog();
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
mNumberProgressBar.setProgress(values[0]);
}
@Override
protected void onCancelled(String aVoid) {
super.onCancelled(aVoid);
dismissDialog();
}
@Override
protected void onCancelled() {
super.onCancelled();
dismissDialog();
}
@Override
protected String doInBackground(String... params) {
String urlStr = params[0];
FileOutputStream output = null;
try {
URL url = new URL(urlStr);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
String qqApkFile = "qqApkFile";
File file = new File(Environment.getExternalStorageDirectory() + "/" + qqApkFile);
if (file.exists()) {
file.delete();
}
file.createNewFile();
InputStream input = connection.getInputStream();
output = new FileOutputStream(file);
int total = connection.getContentLength();
if (total <= 0) {
return null;
}
int plus = 0;
int totalRead = 0;
byte[] buffer = new byte[4*1024];
while((plus = input.read(buffer)) != -1){
output.write(buffer);
totalRead += plus;
publishProgress(totalRead * 100 / total);
if (isCancelled()) {
break;
}
}
output.flush();
} catch (MalformedURLException e) {
e.printStackTrace();
if (output != null) {
try {
output.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
if (output != null) {
try {
output.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
} finally {
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
}
@Override
protected void onPause() {
super.onPause();
if (myTask != null && isFinishing()) {
myTask.cancel(false);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有