簡體   English   中英

Android-僅在AsyncTask未完成的情況下,單擊按鈕上的“顯示進度對話框”

[英]Android - Display Progress Dialog on button click ONLY if AsyncTask has not completed

我正在構建一個OCR Android應用程序,該應用程序在后台執行許多圖像處理任務,這需要一些時間才能完成。 它執行的步驟如下:

  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) {}
}

任何幫助或指導,我們將不勝感激!

只需在Activity或Fragment中使用一個布爾變量,例如

public static boolean isTaskRunning = false;

在AsyncTask的onPreExecute()內部,將其值更改為true。

YourActivity.isTaskRunning = true;

我正在考慮您在活動課中采用了此變量。 在onPostExecute(String result)中,將其值還原為false

YourActivity.isTaskRunning = false;

現在,單擊按鈕,檢查此變量值,如果它為true,則顯示進度對話框,否則不顯示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM