2013-01-24 45 views
0

我有一個調用Web服務,而異步任務執行我想要顯示的加載屏幕解析XML顯示loadingscreen而異步任務

@Override 
protected void onPreExecute(){ 
super.onPreExecute(); 
time = System.currentTimeMillis(); 
} 
protected Boolean doInBackground(Integer... params) { 
    //code 
} 

protected void onPostExecute(Boolean result) { 
super.onPostExecute(result); 
    difftime = System.currentTimeMillis() - time; 
} 

這個異步任務,但加載屏幕異步任務之前完成完成如果我做這樣的

 super.onCreate(savedInstanceState); 
     setContentView(R.layout.loading_screen); 

      final CallWebService callTarif = new CallWebService(6,sett.getDeviceId()); 
      callTarif.execute(); 

new Handler().postDelayed(new Runnable(){ 
     @Override 
      public void run() { 

       LoadingScreen.this.finish(); 
       Intent intent = new Intent(LoadingScreen.this, NextActivity.class); 
            startActivity(intent);    
      } 

     } 
     },callTarif.difftime); 

回答

0

其實postDelayed正在完成的AsyncTask之前調用。

只要把這些代碼行

LoadingScreen.this.finish(); 
Intent intent = new Intent(LoadingScreen.this, NextActivity.class); 
startActivity(intent);  

中的AsyncTask的opPostExecute()

protected void onPostExecute(Boolean result) { 
super.onPostExecute(result); 

    difftime = System.currentTimeMillis() - time; 
    LoadingScreen.this.finish(); 
    Intent intent = new Intent(LoadingScreen.this, NextActivity.class); 
    startActivity(intent);  
} 

,並刪除處理器new Handler().postDelayed(new Runnable(){

0

啓動加載屏幕onPreExecute方法和殺死它onPostExecute異步任務

0

無需使用處理器的顯示使用異步任務訪問webservice時加載的方法。使用AsyncTask的onPreExecute()方法顯示加載屏幕並在onPostExecute內完成,因爲此方法在doInBackground執行完成時調用。將代碼代碼更改爲:

 @Override 
     protected void onPreExecute() { 
      // show loading bar here 
     } 
@Override 
     protected String doInBackground(String... params) { 
       // do network operation here 
      return null; 
     }  

     @Override 
     protected void onPostExecute(String result) {  
      // dismiss loading bar here   
     } 
+0

我必須顯示帶有幻燈片放映的加載屏幕,而不是加載欄 –