1

すべての詳細を含む Class: GalleryLoop があります

私は別のクラスを持っています: AsyncTask を拡張する内部クラスを持つ LoadingScreenActivity

をロードできるように呼び出しようとして.executeいますLoadingScreenActivityGalleryLoop

両方の 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. AsyncTask 内 (do_in_background 内) で GalleryLoop を開始する

前もって感謝します。

4

1 に答える 1

1

この投稿に対する私の答えはあなたを助けるはずです... Asynctask クラスのコンストラクターを使用し、呼び出しアクティビティを Asynctask に渡します...

その後、Asynctask が終了したら、呼び出し元のアクティビティでコールバック関数を呼び出すことができます。

これにより、GalleryLoop アクティビティに 1 つのクラスを使用し、バックグラウンドで Asynctask を使用してロード画面アクティビティを実行し、完了時にコールバック関数を呼び出すことができます。

于 2012-12-18T04:31:12.600 に答える