【问题标题】:How to start an AsyncTask on ActivityResult and show on MainActivity如何在 ActivityResult 上启动 AsyncTask 并在 MainActivity 上显示
【发布时间】:2013-05-17 10:33:33
【问题描述】:

我打开了一个捕获图像的意图,并且 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);
        }
    }
}

【问题讨论】:

  • 在 OnActivityResult 中启动 MainActiviy 并让 MainActivity 启动 AsyncTask
  • 在 OnActivityResult 中启动 MainActivity,在 MainActivity onCreate() 中启动 AsyncTask。
  • 如何在 onActivityResult 中启动 MainActivity?
  • @GeorgeMelidis 您指的是“MainActivity”,是当前具有 onActivityResult().. 的活动吗?
  • 发布你尝试过的源代码。

标签: android android-intent android-asynctask main-activity


【解决方案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);

等活动结果返回您的处理在后台开始

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多