3

I open an intent for capture an image and OnActivityResult i call an AsyncTask to start to make some processing to the image. The problem is that i want to load first the mainActivity and then start the AsyncTask in order to see the percentage of 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);
        }
    }
}
George Melidis
  • 599
  • 3
  • 9
  • 25
  • in OnActivityResult start the MainActiviy and let the MainActivity starts the AsyncTask – Blackbelt May 17 '13 at 10:35
  • Start MainActivity in OnActivityResult and Start AsyncTask in MainActivity onCreate(). – Manish Dubey May 17 '13 at 10:36
  • how i start the MainActivity in onActivityResult? – George Melidis May 17 '13 at 10:36
  • @GeorgeMelidis Is you 'MainActivity' that you are referring to is current activity having onActivityResult()..? – Arpit Garg May 17 '13 at 10:39
  • 2
    Post you source code what you have tried. – Manish Dubey May 17 '13 at 10:40
  • @ArpitGarg I don't understand what you mean..can you explain it better? – George Melidis May 17 '13 at 10:45
  • @GeorgeMelidis I mean do you just need to start an another activity in onActivityResult..? So, what the big deal in that. – Arpit Garg May 17 '13 at 10:48
  • @ManishAndroid I posted my code – George Melidis May 17 '13 at 10:57
  • @GeorgeMelidis : Use prvn's answer, it will work. – Manish Dubey May 17 '13 at 11:07
  • @ArpitGarg The big deal my friend is not to start an another activity onActivityResult(you mean the Main Activity) but to be able to see the percentage of AsyncTask when the AsyncTask is executed from OnActivityResult. Because until all the commands are executed onActivityResult I see black screen and when OnActivityResult is finished i see the async task at 75 percent finishing and then get back to mainActivity. So, do you have a suggestion in order to be able to see asyncTask percentage from the start? That's why i thought how to get the mainScreen of mainActivity and then start AsyncTask. – George Melidis May 17 '13 at 11:26
  • @GeorgeMelidis Make sure the commands in onActivityResult are not that more. If so also puts those extra stuff in your doInBackground(). SO that the calling is returned instantaneously, and AsynsTask show updates without lacking. Also start the progress showing in onPreExecute() only. DO NEVER CALL aTask.doInBackground() . USE aTask.execute() ONLY. ELSE YOUR OnPreExe() and onProgressUpdate() and onPostExecute() will not work synchronously. I hope m clear to what I want to convey. – Arpit Garg May 17 '13 at 11:59

1 Answers1

1

Have this in your 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

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

so on activity result is returned your processing is started in background

prvn
  • 916
  • 5
  • 5