简体   繁体   English

如何在ActivityResult上启动AsyncTask并在MainActivity上显示

[英]How to start an AsyncTask on ActivityResult and show on MainActivity

I open an intent for capture an image and OnActivityResult i call an AsyncTask to start to make some processing to the image. 我打开捕获图像的意图和OnActivityResult我调用AsyncTask开始对图像进行一些处理。 The problem is that i want to load first the mainActivity and then start the AsyncTask in order to see the percentage of AsyncTask. 问题是我想首先加载mainActivity,然后启动AsyncTask以查看AsyncTask的百分比。 How can i do that? 我怎样才能做到这一点?

@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);
        }
    }
}

Have this in your MainActivity 在您的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();
    }
}

inside your OnActivityResult method add this 在你的OnActivityResult方法中添加这个

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

so on activity result is returned your processing is started in background 所以在活动结果返回后,您的处理将在后台启动

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM