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)my question: When I run this function, there will be RuntimeException(No Looper; Looper.prepare() wasn't called on this thread.);

So I changed:

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);
    }

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

(2) Question: How to achieve access to the network to update information every 1 hour?

Cœur
  • 37,241
  • 25
  • 195
  • 267
see2851
  • 627
  • 1
  • 9
  • 13
  • 1
    **"Question:How to achieve access to the network to update information every 1 hour?"** - Look at using an `IntentService` triggered by a repeating alarm using `AlarmManager`. – Squonk Dec 25 '12 at 02:04
  • You have to use `runOnUiThread`. http://stackoverflow.com/questions/10135353/when-may-we-need-to-use-runonuithread-in-android-applciation – mihirjoshi Dec 25 '12 at 04:58

1 Answers1

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.

Nitish
  • 3,097
  • 13
  • 45
  • 80