2

I'm trying to create an Android application that shows the current time. I want to update time on my Activity with the Timer, but the TextView is not updating, so just one time is always on the screen. Here is my code:

package com.example.androidtemp;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import com.example.androidtemp.R;

public class ActivityTime extends Activity
{
    SimpleDateFormat sdf;
    String time;
    TextView tvTime;
    String TAG = "States";

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity_time);

        sdf = new SimpleDateFormat("HH:mm:ss");
        time = sdf.format(new Date(System.currentTimeMillis()));

        tvTime = (TextView) findViewById(R.id.tvTime);

        Timer timer = new Timer();
        TimerTask task = new TimerTask()
        {
            @Override
            public void run()
            {
                // TODO Auto-generated method stub
                timerMethod();
            }
        };

        try
        {
            timer.schedule(task, 0, 1000);
        } 
        catch (IllegalStateException e)
        {
            // TODO: handle exception
            e.printStackTrace();
            Log.e(TAG, "The Timer has been canceled, or if the task has been scheduled or canceled.");
        }
    }

    protected void timerMethod()
    {
        // TODO Auto-generated method stub
        this.runOnUiThread(changeTime);
    }

    private  final Runnable changeTime = new Runnable()
    {
        public void run()
        {
            // TODO Auto-generated method stub
            //Log.d(TAG, "Changing time.");
            sdf.format(new Date(System.currentTimeMillis()));
            tvTime.setText(time);
        }
    };
}

Does anyone have solution for this problem?

yyunikov
  • 5,719
  • 2
  • 43
  • 78

2 Answers2

0

Use a handler since it can access the views of your application. The views of your application belong to the main thread already so creating another thread to access them usually doesn't work. If am not wrong, handlers use messages to communicate with the main thread and its components. Use this where you have your thread definition instead:

Handler  handler = new Handler();
handler.removeCallbacks(runnable);
handler.postDelayed(runnable, 1000);

and add this to your runnable definition

handler.postDelayed(runnable, 1000);

This last statement removes any instance of runnable awaiting execution when a new on is added. Kind of like clearing a queue.

Kevin Murani
  • 186
  • 1
  • 5
0

I would recommend you to use DigitalClock or TextClock (you can use 'include' in the layout xml and layout / layout-v17 to use a different component depending of the OS version) if you just want to show the time.

If you want to have more control, I would recommend you using a Handler or an ExecutorService instead of a Timer. Java Timer vs ExecutorService?

If you want to fix your code as it is, just modify the value of the variable 'time' ;)

Community
  • 1
  • 1