2012-12-18 18 views
1

我有一個類:GalleryLoop包含所有細節調用從發佈活動在的AsyncTask執行

我有一個單獨的類別:LoadingScreenActivity是具有延伸的AsyncTask

我想一個內部類調用.executeLoadingScreenActivity,這樣我就可以加載GalleryLoop

有沒有什麼辦法可以在不組合兩個Java CLass文件的情況下執行?

的代碼如下:

private class LoadViewTask extends AsyncTask<Void, Integer, Void> 
    { 
     //A TextView object and a ProgressBar object 
     private TextView tv_progress; 
     private ProgressBar pb_progressBar; 

     //Before running code in the separate thread 
     @Override 
     protected void onPreExecute() 
     { 
      //Initialize the ViewSwitcher object 
      viewSwitcher = new ViewSwitcher(LoadingScreenActivity.this); 
      /* Initialize the loading screen with data from the 'loadingscreen.xml' layout xml file. 
      * Add the initialized View to the viewSwitcher.*/ 
      viewSwitcher.addView(ViewSwitcher.inflate(LoadingScreenActivity.this, R.layout.loadingscreen, null)); 

      //Initialize the TextView and ProgressBar instances - IMPORTANT: call findViewById() from viewSwitcher. 
      tv_progress = (TextView) viewSwitcher.findViewById(R.id.tv_progress); 
      pb_progressBar = (ProgressBar) viewSwitcher.findViewById(R.id.pb_progressbar); 
      //Sets the maximum value of the progress bar to 100    
      pb_progressBar.setMax(100); 

      //Set ViewSwitcher instance as the current View. 
      setContentView(viewSwitcher); 
     } 

     //The code to be executed in a background thread. 
     @Override 
     protected Void doInBackground(Void... params) 
     { 
      /* This is just a code that delays the thread execution 4 times, 
      * during 850 milliseconds and updates the current progress. This 
      * is where the code that is going to be executed on a background 
      * thread must be placed. 
      */ 
      try 
      { 
       //Get the current thread's token 
       synchronized (this) 
       { 
        //Initialize an integer (that will act as a counter) to zero 
        int counter = 0; 
        //While the counter is smaller than four 
        while(counter <= 4) 
        { 
         //Wait 850 milliseconds 
         this.wait(850); 
         //Increment the counter 
         counter++; 
         //Set the current progress. 
         //This value is going to be passed to the onProgressUpdate() method. 
         publishProgress(counter*25); 
        } 
       } 
      } 
      catch (InterruptedException e) 
      { 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     //Update the TextView and the progress at progress bar 
     @Override 
     protected void onProgressUpdate(Integer... values) 
     { 
      //Update the progress at the UI if progress value is smaller than 100 
      if(values[0] <= 100) 
      { 
       tv_progress.setText("Progress: " + Integer.toString(values[0]) + "%"); 
       pb_progressBar.setProgress(values[0]); 
      } 
     } 

     //After executing the code in the thread 
     @Override 
     protected void onPostExecute(Void result) 
     { 
      /* Initialize the application's main interface from the 'main.xml' layout xml file. 
      * Add the initialized View to the viewSwitcher.*/ 
      viewSwitcher.addView(ViewSwitcher.inflate(LoadingScreenActivity.this, R.layout.main, null)); 
      //Switch the Views 
      viewSwitcher.showNext(); 
      //ImageView = viewSwitcher.findViewById(R.id.imageView1); 
      setContentView(R.layout.main); 
     } 
    } 

其他類文件:

public class GalleryLoop extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
//details of Galleryloop 
} 

我是新來的Android,我不知道哪幾種情況會是更好的選擇:

1.在AsyncTask啓動前啓動GalleryLoop

2.Initi AtingTask內的GalleryLoop(在do_in_background中)

在此先感謝。

+0

是的,它可能只是創建LoadViewTask類作爲單獨的調用,然後擴展AsyncTask –

+0

我可以知道如何去做呢? – user1856686

+0

我已經有一個LoadViewTask類並且擴展到AsyncTask。不過,我希望能夠在後執行函數上加載GalleryLoop。我想創建另一個類或? – user1856686

回答

1

我的回答this post應該幫助你只能使用一個構造函數的AsyncTask類,並通過調用活動到的AsyncTask ...

然後就可以調用在調用活動的回調函數一次的AsyncTask是完了。

這應該允許您爲GalleryLoop活動使用一個類,並在後臺使用Asynctask運行加載屏幕活動,並在完成時調用回調函數。

+0

如果我的preexecute正在使用viewswitcher,doInBackground正在加載進度條,這是否意味着我的後期執行中,我需要使用viewswitcher將視圖更改爲GalleryLoop,或者我需要刪除視圖切換器,關閉進度欄並加載GalleryLoop?我很困惑如何去做。 – user1856686