gpt4 book ai didi

java - 从 AsyncTask ProgressDialog 取消 AsyncTask

转载 作者:行者123 更新时间:2023-12-01 20:57:05 26 4
gpt4 key购买 nike

(这与空指针无关):我在 AsyncTask 中有一个进度条,并且添加了一个取消按钮来取消 asynctask。

我可以从异步任务外部取消异步任务,但我需要在异步任务下实现的progressdialog中实现取消功能。

那么问题是如何使用asynctask下progressdialog中实现的取消按钮来取消asynctask?

请检查“doInBackground”..异步任务未取消

下载_result.java类:

public class Download_result extends AsyncTask<String,Integer,Void>{
ProgressDialog progressDialog;
Context context;
String pdfFile;


Download_result(Context context, String pdfFile){
this.context=context;
this.pdfFile=pdfFile;
}
@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(context);
progressDialog.setTitle("Downloading...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMax(200);
progressDialog.setCancelable(false);
progressDialog.setProgress(0);
progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Download_result.this.cancel(true);
dialog.dismiss();
}
});
progressDialog.show();
}

@Override
protected Void doInBackground(String... params) {
//given below
}
@Override
protected void onProgressUpdate(Integer... values) {
progressDialog.setProgress(values[0]);
}

@Override
protected void onPostExecute(Void result) {
progressDialog.cancel();

}
}

enter image description here

我的“doInBackground”方法:

@Override
protected Void doInBackground(String... params) {

String url_1=params[0];
int file_length=0;
try {
URL url = new URL(url_1);
URLConnection urlConnection = url.openConnection();
urlConnection.connect();
file_length=urlConnection.getContentLength();
filesize=file_length;
File sdCard = Environment.getExternalStorageDirectory();
File new_folder = new File (sdCard.getAbsolutePath() + "/xxx");


File input_file = new File(new_folder,pdfFile);
InputStream inputStream = new BufferedInputStream(url.openStream(),8192);
byte[] data=new byte[1024];
int total=0,count=0;
OutputStream outputStream = new FileOutputStream(input_file);
while ((count=inputStream.read(data))!=-1){
total+=count;
outputStream.write(data,0,count);


int progress= (total*200)/file_length;
downloadedsize=total;

publishProgress(progress);
if(isCancelled()){
break; or return null; // same result
}


}
inputStream.close();
outputStream.close();


} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;//"Download completed!";
}

最佳答案

您尚未在按下取消按钮时关闭对话框。也可以使用 setButton 代替

试试这个:

 @Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(context);
progressDialog.setTitle("Downloading...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMax(200);
progressDialog.setCancelable(false);
progressDialog.setProgress(0);
progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
download.cancel(true);
downloadstatus=false; //add boolean check
dialog.dismiss();
}
});
progressDialog.show();
}

要取消异步任务,请使用:

 Public Download_result download;
download = new Download_result();
download.execute();
download.cancel(true);

doInbackGround()中试试这个

 while ((count=inputStream.read(data))!=-1){

if(!your_AsyncTask.isCancelled() || downloadstatus !=false){
total+=count;
outputStream.write(data,0,count);
int progress= (total*200)/file_length;
downloadedsize=total;

publishProgress(progress);
}else{
break;
}
}

关于java - 从 AsyncTask ProgressDialog 取消 AsyncTask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42185296/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com