Android - 仅当 AsyncTask 尚未完成时,才会在单击按钮时显示进度对话框
Android - Display Progress Dialog on button click ONLY if AsyncTask has not completed
我正在构建一个 OCR Android 应用程序,它在后台执行许多需要一些时间才能完成的图像处理任务。
它执行的步骤如下:
- 拍摄图像
- 向用户显示图像,提供重新捕获图像或继续的选项
- 显示处理后的图像,提供重新捕获图像或继续的选项。
- 提取文本
这些任务很耗时,我想通过在前一个任务完成后立即开始下一个任务来减少一些时间,同时显示进度对话框 "Please wait" 仅当用户单击继续按钮和任务尚未完成。
我想知道这是否可能,如果可以,我该如何实现?
下面是我的 OCR 任务代码:
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) {}
}
非常感谢任何帮助或指导!
只需在 Activity 或 Fragment 中取一个布尔变量,例如
public static boolean isTaskRunning = false;
并在 AsyncTask 的 onPreExecute() 内部,将其值更改为 true。
YourActivity.isTaskRunning = true;
我考虑到您在 activity class 中使用了这个变量。在 onPostExecute(String result) 内部,将其值恢复为 false
YourActivity.isTaskRunning = false;
现在,点击按钮,检查这个变量值,如果它 true 然后显示你的 progress dialog 否则不是。
我正在构建一个 OCR Android 应用程序,它在后台执行许多需要一些时间才能完成的图像处理任务。 它执行的步骤如下:
- 拍摄图像
- 向用户显示图像,提供重新捕获图像或继续的选项
- 显示处理后的图像,提供重新捕获图像或继续的选项。
- 提取文本
这些任务很耗时,我想通过在前一个任务完成后立即开始下一个任务来减少一些时间,同时显示进度对话框 "Please wait" 仅当用户单击继续按钮和任务尚未完成。
我想知道这是否可能,如果可以,我该如何实现?
下面是我的 OCR 任务代码:
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) {}
}
非常感谢任何帮助或指导!
只需在 Activity 或 Fragment 中取一个布尔变量,例如
public static boolean isTaskRunning = false;
并在 AsyncTask 的 onPreExecute() 内部,将其值更改为 true。
YourActivity.isTaskRunning = true;
我考虑到您在 activity class 中使用了这个变量。在 onPostExecute(String result) 内部,将其值恢复为 false
YourActivity.isTaskRunning = false;
现在,点击按钮,检查这个变量值,如果它 true 然后显示你的 progress dialog 否则不是。