Android-仅当AsyncTask尚未完成时,单击按钮时显示进度对话框
我正在构建一个OCR Android应用程序,它在后台执行许多图像处理任务,这需要一些时间才能完成。 它执行的步骤如下:Android-仅当AsyncTask尚未完成时,单击按钮时显示进度对话框,android,android-asynctask,progressdialog,Android,Android Asynctask,Progressdialog,我正在构建一个OCR Android应用程序,它在后台执行许多图像处理任务,这需要一些时间才能完成。 它执行的步骤如下: 捕获图像 向用户显示图像,提供重新捕获图像或继续的选项 显示处理后的图像,提供重新捕获图像或继续的选项 提取文本 这些任务非常耗时,我想通过在上一个任务完成后立即开始下一个任务来减少一些时间,同时仅当用户单击“继续”按钮且任务尚未完成时才显示进度对话框“请稍候” 我想知道这是否可能,如果可能,我如何实现这一点 以下是我的OCR任务代码: private class OCRTa
private class OCRTask extends AsyncTask<Void, Void, String> {
ProgressDialog mProgressDialog;
public OCRTask(PreviewActivity activity) {
mProgressDialog = new ProgressDialog(activity);
}
@Override
protected String doInBackground(Void... params) {
String path = previewFilePath;
String ocrText;
OCR ocr = new OCR();
ocrText = ocr.OCRImage(path, PreviewActivity.this);
// Write the result to a txt file and store it in the same dir as the temp img
// find the last occurence of '/'
int p=previewFilePath.lastIndexOf("/");
// e is the string value after the last occurence of '/'
String e=previewFilePath.substring(p+1);
// split the string at the value of e to remove the it from the string and get the dir path
String[] a = previewFilePath.split(e);
String dirPath = a[0];
String fileString = dirPath + "ocrtext.txt";
File file = new File(fileString);
try {
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(ocrText);
bw.close();
System.out.println("done!!");
} catch (IOException i) {
i.printStackTrace();
}
new WordCorrect(fileString);
return ocrText;
}
@Override
protected void onPreExecute() {
// Set the progress dialog attributes
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setMessage("Extracting text...");
mProgressDialog.show();
}
@Override
protected void onPostExecute(String result) {
// dismiss the progress dialog
mProgressDialog.dismiss();
Intent i;
i = new Intent(PreviewActivity.this, ReceiptEditActivity.class);
// Pass the file path and text result to the receipt edit activity
i.putExtra(FILE_PATH, previewFilePath);
Log.e("OCR TEXT: ", result);
i.putExtra(OCR_TEXT, result);
// Start receipt edit activity
PreviewActivity.this.startActivityForResult(i, 111);
finish();
}
@Override
protected void onProgressUpdate(Void... values) {}
}
私有类OCRTask扩展了异步任务{
进程对话框;
公共任务(预览活动活动){
mProgressDialog=新建进度对话框(活动);
}
@凌驾
受保护字符串doInBackground(无效…参数){
字符串路径=预览文件路径;
字符串文本;
OCR OCR=新的OCR();
ocrText=ocr.OCRImage(路径,PreviewActivity.this);
//将结果写入txt文件,并将其存储在与temp img相同的目录中
//查找“/”的最后一次出现
int p=previewFilePath.lastIndexOf(“/”);
//e是上次出现“/”后的字符串值
字符串e=previewFilePath.substring(p+1);
//在值e处拆分字符串以将其从字符串中移除,并获取dir路径
字符串[]a=previewFilePath.split(e);
字符串dirPath=a[0];
字符串fileString=dirPath+“ocrtext.txt”;
File File=新文件(fileString);
试一试{
FileWriter fw=新的FileWriter(文件);
BufferedWriter bw=新的BufferedWriter(fw);
写(文本);
bw.close();
System.out.println(“完成!!”;
}捕获(IOI异常){
i、 printStackTrace();
}
新的WordCorrect(fileString);
返回文本;
}
@凌驾
受保护的void onPreExecute(){
//设置进度对话框属性
mProgressDialog.setProgressStyle(ProgressDialog.STYLE\u微调器);
设置消息(“提取文本…”);
mProgressDialog.show();
}
@凌驾
受保护的void onPostExecute(字符串结果){
//关闭进度对话框
mProgressDialog.disclose();
意图一;
i=新意图(PreviewActivity.this、ReceiptEditActivity.class);
//将文件路径和文本结果传递给收据编辑活动
i、 putExtra(文件路径、预览文件路径);
Log.e(“OCR文本:”,结果);
i、 putExtra(OCR_文本、结果);
//开始收据编辑活动
PreviewActivity.this.startActivityForResult(i,111);
完成();
}
@凌驾
受保护的void onProgressUpdate(void…值){}
}
非常感谢任何帮助或指导 只需在活动或片段中取一个布尔变量,如
public static boolean isTaskRunning = false;
在AsyncTask的onPreExecute()中,将其值更改为true
YourActivity.isTaskRunning = true;
我认为你在活动课上学习过这个变量。在onPostExecute(字符串结果)中,将其值还原为false
YourActivity.isTaskRunning = false;
现在,单击按钮,检查此变量值,如果其为真,则显示进度对话框,否则不显示