The following code shows how to Update TextView with a Thread.
\res\layout\main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout>
MainActivity.java
//from w ww . ja v a 2 s .co m package com.java2s.myapplication3.app; import java.util.Calendar; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.widget.TextView; public class MainActivity extends Activity { TextView mClock; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mClock = new TextView(this); setContentView(mClock); } private Handler mHandler = new Handler(); private Runnable timerTask = new Runnable() { @Override public void run() { Calendar now = Calendar.getInstance(); mClock.setText(String.format("%02d:%02d:%02d", now.get(Calendar.HOUR), now.get(Calendar.MINUTE), now.get(Calendar.SECOND)) ); mHandler.postDelayed(timerTask,1000); } }; @Override public void onResume() { super.onResume(); mHandler.post(timerTask); } @Override public void onPause() { super.onPause(); mHandler.removeCallbacks(timerTask); } }