我正在构建一个OCR Android应用程序,它在后台执行许多图像处理任务,需要一些时间才能完成。 它执行的步骤如下:
这些任务非常耗时,我希望通过在上一个任务完成后立即启动下一个任务来减少一些时间,同时仅在用户单击“继续”按钮并且任务具有任务时显示进度对话框“请稍候”没完成。
我想知道这是否可能,如果是这样,我该如何实现?
以下是我的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) {}
}
非常感谢任何帮助或指导!
答案 0 :(得分:0)
只需在Activity或Fragment中包含一个布尔变量,例如
public static boolean isTaskRunning = false;
并在AsyncTask的 onPreExecute()内,将其值更改为true。
YourActivity.isTaskRunning = true;
我正在考虑你已经在活动类中使用了这个变量。在onPostExecute(String result)中,将其值恢复为false
YourActivity.isTaskRunning = false;
现在,点击按钮,检查此变量值,如果 true ,则显示进度对话框,否则不显示。