From a28bb6c7a3376b82a726835bbc36f32031c4b095 Mon Sep 17 00:00:00 2001 From: Eugene Crosser Date: Wed, 25 Jan 2012 22:04:38 +0400 Subject: [PATCH] use Handler.postDelayed for repeated task --- src/org/average/whereami/WhereAmI.java | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/org/average/whereami/WhereAmI.java b/src/org/average/whereami/WhereAmI.java index 2f69212..1186793 100644 --- a/src/org/average/whereami/WhereAmI.java +++ b/src/org/average/whereami/WhereAmI.java @@ -3,6 +3,8 @@ package org.average.whereami; import android.app.Activity; import android.os.Bundle; import android.os.AsyncTask; +import android.os.Handler; +import android.os.SystemClock; import android.util.Log; import android.view.View; import android.view.Window; @@ -11,6 +13,8 @@ import android.widget.TextView; public class WhereAmI extends Activity { + private TextView tv; + /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) @@ -20,11 +24,24 @@ public class WhereAmI extends Activity getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); setContentView(R.layout.main); + tv = (TextView)findViewById(R.id.tv); Log.v("WhereAmI", "created UI, about to start update task"); - new TimedUpdateTask().execute(5); + mHandler.postDelayed(updateClock, 2000); Log.v("WhereAmI", "created UI, update task created"); } + private Handler mHandler = new Handler(); + + private Runnable updateClock = new Runnable () { + public void run() { + long millis = System.currentTimeMillis(); + Log.v("updateClock", "executing"); + // new TimedUpdateTask().execute(5); // for delayed execution + tv.setText(Long.toString(millis)); + mHandler.postDelayed(this, 2000); + } + }; + private class TimedUpdateTask extends AsyncTask { @Override protected String doInBackground(Integer... howlong) { @@ -40,9 +57,8 @@ public class WhereAmI extends Activity @Override protected void onPostExecute(String str) { - TextView tv = (TextView)findViewById(R.id.tv); Log.v("TimedUpdateTask", "callback executing"); - tv.append("\n" + str); + tv.setText(str); } } } -- 2.43.0