2017-04-07 34 views
0

我正在构建一个OCR Android应用程序,它在后台执行很多图像处理任务,需要一些时间才能完成。是 它执行的步骤如下:Android - 显示进度对话框on按钮只有在AsyncTask没有完成时才点击

  1. 捕获图像
  2. 显示图像对用户而言,提供选项以重新捕获图像或进行
  3. 显示处理后的图像,提供选项以重新捕获图像或进行。
  4. 提取文本

这些任务耗费时间,我想通过尽快以前完成启动下一个任务,以减少一些时间,同时显示进度对话框“请稍候”,只有当用户点击继续按钮,任务尚未完成。

我想知道这是可能的,如果是的话,我该如何做到这一点?

下面是我的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

就拿一个布尔变量的活动或片段里面,像

public static boolean isTaskRunning = false; 

和内部onPreExecute()的AsyncTask的,它的值更改为true。

YourActivity.isTaskRunning = true; 

我在考虑你在活动课上采用了这个变量。而且里面onPostExecute(字符串结果),恢复它的值设置为false

YourActivity.isTaskRunning = false; 

现在,按一下按钮,检查这个变量的值,如果其真正然后显示您的进度对话框否则不是。

+0

谢谢你,完美的工作! –

相关问题