3

我打开了一个捕获图像的意图,并且 OnActivityResult 我调用了一个 AsyncTask 来开始对图像进行一些处理。问题是我想先加载 mainActivity 然后启动 AsyncTask 以查看 AsyncTask 的百分比。我怎样才能做到这一点?

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
           try {
             rotationIfNeeded();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Log.v(TAG, "C");

            retrievePreferences();
            OcrAsyncTask aTask = new OcrAsyncTask(dialog, languageCode, languagePath, bitmap);
            aTask.execute(languagePath);

            while (aTask.doInBackground(languagePath)){
                //Do Nothing
            }

            recognizedText = aTask.getRecognizedText();
            Log.v(TAG, "The Recognized Text is = " + aTask.getRecognizedText());
            if ( recognizedText.length() != 0 ) {
                ocrResult.setText(ocrResult.getText().toString().length() == 0 ? recognizedText : ocrResult.getText() + " " + recognizedText);
                ocrResult.setSelection(ocrResult.getText().toString().length());
            }

        } else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
        } else {
            Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
        }
    }
}
4

1 回答 1

1

在你的 MainActivity 中有这个

private class MyAsyn extends AsyncTask<Boolean, Void, Object> {
    private ProgressDialog pd;

    /** application context. */
    public MyAsyn(Activity activity) {
        pd = new ProgressDialog(activity);
    }

    protected void onPreExecute() {
        pd.setMessage("Loading.. Please wait");
        pd.show();
    }

    @Override
    protected String doInBackground(Boolean... urls) {

        pd.show();

        // do what you want to do in back ground

        return null;
    }

            @Override
    protected void onProgressUpdate(Void... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
                    // code to notify the progress
    }

    @Override
    protected void onPostExecute(Object result) {
        pd.dismiss();
    }
}

在你的 OnActivityResult 方法中添加这个

MyAsyn task = new MyAsyn(MainActivity.this);
task.execute(true);

所以返回活动结果您的处理是在后台开始的

于 2013-05-17T10:50:49.217 回答