2010-12-10 41 views
0

如何在android中添加dobarbackground asynctask中的tabbar?或者如何在線程中運行啓動畫面?我想顯示一個進度對話框或啓動畫面,直到從調用tabbar類加載標籤欄我打電話給webservices和解析值需要時間,幾分鐘的平均時間,我必須顯示progessbar或閃屏。任何人都可以告訴我如何實現這個?任何人都可以提供示例代碼? 我試過,但它不工作如何在doinbackground asynctask中添加tabbar android

dlg = ProgressDialog.show(this, "Working..", "Downloading Data...", true, false);  

Thread splashThread = new Thread() { 
     @Override 
     public void run() { 
      try { 

       sleep(100000); 



      } catch (InterruptedException e) { 
       // do nothing 
      } finally { 

      } 
     } 
    }; 
    splashThread.start(); 

回答

0

你不能從後臺線程觸摸主UI線程的事情,爲了做到這一點,你必須使用誰管理後臺線程和UI主線程之間的同步處理程序。

更好的方法是繼承AsyncTask,這樣做,你已經擁有了處理UI的背景和方法的方法。有了這個,你可以進行後臺操作和inmediatly顯示結果...

public class AsynchronousTask extends AsyncTask<Runnable, String, Result> { 

//method executed automatically in the ui event before the background thread execution 
@Override 
    protected void onPreExecute() { 
     //show the splash screen and add a progress bar indeterminate 
    } 

//method executed automatically in the ui event AFTER the background thread execution 
@Override 
    protected void onPostExecute(Result result) { 
     //hide the splash screen and drop the progress bar or set its visibility to gone. 
    } 

//method executed in a background event when you call explicitly execute... 
@Override 
    protected Result doInBackground(Runnable... tasks) { 
Result result; 
     if(tasks != null && tasks.length > 0){ 
      for (Runnable runnable : tasks) { 
       //publishProgress(...); 
       runnable.run(); 
           //result = ... 
       //publishProgress(...); 
      } 
     } 
     return result; 
    } 

} 

http://developer.android.com/reference/android/os/AsyncTask.html

在這裏閱讀更多 希望這有助於...

+0

什麼是返回類型,我會給舉例 – mohan 2010-12-13 14:17:44

+0

內部的方法onPreExecute和onPostExecute你必須顯示AlertDialog,刷新你的啓動畫面等...因爲這些方法在事件線程中運行,如果你重寫onProgressUpdate(int progress),你可以顯示一個ProgressBar進度更新。這最後也在UI中運行。 – Franco 2010-12-13 21:16:58

相關問題