0
private void startUpdateTimerTask() {
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                doUpdate();

            }
        };

        Timer timer = new Timer(true);
        timer.schedule(task, ONE_MINUTE_MILLIS, ONE_HOUR_MILLIS);
    }

        private void doUpdate() { 
              new AsyncTask<Void,Void,Void>() {

            @Override
            protected Void doInBackground(Void... params) { 

                //....Network time-consuming tasks
                return null;
            }

        }.equals();

        }

(1)私の質問: この関数を実行すると、RuntimeException(No Looper; Looper.prepare() was not called on this thread.); が発生します。

だから私は変更しました:

private void startUpdateTimerTask() {
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                         Looper.prepare();

                 doUpdate();

                         Looper.loop()

            }
        };

        Timer timer = new Timer(true);
        timer.schedule(task, ONE_MINUTE_MILLIS, ONE_HOUR_MILLIS);
    }

するとRuntimeExceptionは出ませんが、doUpdate()は一度しか実行されませんか?

(2)質問: 1 時間ごとに情報を更新するためにネットワークにアクセスするにはどうすればよいですか?

4

1 に答える 1

0

then RuntimeException does not appear, but doUpdate() Executed only once?

This is because an asynctask can execute only once.The doInBackground() runs on a separate thread, and once a thread has completed its process, you cannot start it again. Since you are already using timer task, the timer task performs operation on separate worker thread, so you can perform the same operation in the run() of timer task, which you are performing in doInBackground() of AsyncTask. For updating your UI, you can make use of Runnable.

于 2012-12-25T02:34:45.907 に答える