2013-05-17 75 views
3

我打開一個捕獲圖像和OnActivityResult的意圖,我調用一個AsyncTask開始對圖像進行一些處理。問題是我想先加載mainActivity,然後啓動AsyncTask以查看AsyncTask的百分比。我怎樣才能做到這一點?如何在ActivityResult上啓動一個AsyncTask並在MainActivity上顯示

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

開始MainActiviy並讓MainActivity啓動AsyncTask – Blackbelt

+0

開始Mai OnActivityResult中的nActivity和MainActivity onCreate()中的啓動AsyncTask。 –

+0

我如何在onActivityResult中啓動MainActivity? –

回答

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

等活動結果返回您的處理,由OnActivityResult啓動背景

相關問題