2013-06-22 55 views
0

我正在android應用程序上工作。我需要從onpostexecute方法中獲取值。即我需要使用onpostexecute方法的返回值執行一些任務。我怎樣才能做到這一點?在這方面請幫助我。如何從android中的onpostexecute方法獲取返回值?

我的代碼:

boolean val= false; 


    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     LongOperation1 op = new LongOperation1(); 
      op.execute(""); 
//I want the value here again 


} 

private class LongOperation1 extends AsyncTask<String, Void, String> { 

     @Override 
     protected String doInBackground(String... params) { 

      loadFeed2(); 
      return "Executed"; 
     } 
      @Override 
     protected void onPostExecute(String result) { 
      dialog1.dismiss(); 
      try { 
        dialog.dismiss(); 
//mystuff 
       val = true; 


     } 
      catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     @Override 
     protected void onPreExecute() { 
      dialog1 = ProgressDialog.show(Taketest.this, "Please wait...", 
        "Retrieving data ...", true); 
     } 

     @Override 
     protected void onProgressUpdate(Void... values) { 
     } 
    } 
+0

使用全局變量 – KOTIOS

+0

調用內部postexecute(功能),傳遞價值該函數的參數 – Senthil

+0

hmm是我使用全局變量....在onpostexecute方法我將值更改爲true。現在我想在onc​​reate方法中使用它。我怎樣才能得到這個價值? – Amrutha

回答

1

ü可以嘗試這樣的事情, 使布爾VAL全球然後

Public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    LongOperation1 op = new LongOperation1(); 
    op.execute(""); 
    // here should not check val is true or not because it will run before ansynch complete 
} 

public void init() 
{ 
if(val) 
     { 

    Log.e("val is","true"); 
     } 

} 

........... 

    @Override 
    protected void onPostExecute(String result) { 
     dialog1.dismiss(); 
     try { 
       dialog.dismiss(); 
    //mystuff 
      val = true; 

      init(); 


    } 
相關問題