2012-12-18 1 views
1

나는 클래스가 .executeGalleryLoop은 골대 활동은 AsyncTask를에서 실행 호출

등으로 호출 할 수 있습니다. 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 
} 

나는 안드로이드에 새로운 오전과 내가 더 나은 선택이 될 것입니다 다음 중 확실하지 않다 :

1. AsyncTask가 시작되기 전에 GalleryLoop을 시작함으로써

2.Initi AsyncTask 내 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가 진행 막대를로드합니다. postexecute에서 viewswitcher를 사용하여보기를 GalleryLoop로 변경해야하거나 viewswitcher를 제거하고 진행 막대를 닫은 다음로드해야합니다. GalleryLoop? 나는 그것을하는 방법에 관해서 꽤 혼란 스럽다. – user1856686