0

I want to create a clock showing the hour and Minute of the current time like this 02:15 PM. What i want is now to keep the minutes part updating obviously after every 60 seconds as it is changed in our systems. So i want to know what is the best way to keep the time updating thanks

CodeAddiction
  • 89
  • 2
  • 12
  • what about,resetting your clock after 60 seconds. – subodh Mar 14 '13 at 06:53
  • What i am doing is getting the date time from API which returns me different countries date time, so i have to actually recall that function after every 60 seconds to update the date time or any other good way you recommend – CodeAddiction Mar 14 '13 at 06:59
  • Try this [http://stackoverflow.com/questions/2535733/display-data-after-every-10-seconds-in-android] – subodh Mar 14 '13 at 07:05

1 Answers1

0

.xml file's code,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/txtTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

</RelativeLayout>

Activity file's code,

package com.example.demoproject;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.text.format.Time;
import android.widget.TextView;

public class MainActivity extends Activity 
{
    private static TextView textview;
    private Timer timer;
    private Time today;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textview = (TextView ) findViewById( R.id.txtTime );

        today = new Time(Time.getCurrentTimezone());
        timer = new Timer();
        timer.schedule(new RemindTask(), 1000, 1000 );
    }

    private class RemindTask extends TimerTask
    {
        public void run()
        {

            runOnUiThread(new Runnable() 
            {
                 public void run() 
                 {
                        today.setToNow();
                        textview.setText(today.format("%k:%M:%S"));  // Current time
                }
            });
        }
    }
}
Raynold
  • 443
  • 2
  • 9
  • 28
  • That is one second and won't be synchronized with the clock. – assylias Mar 14 '13 at 07:00
  • @assylias, how about now ? – Raynold Mar 14 '13 at 07:01
  • Again thanks Raynold, I want to know that should i put the timer task in separate package or i can put it in the main activity. I am asking this because i have tried the following code but it did not worked for me. In timer task run() function i want to re create the UI of my application to get different and updated date time plus the weather update. All the code is working except the updating code. Guide me if you got me :) – CodeAddiction Mar 14 '13 at 07:02
  • you can use in same activity class – Raynold Mar 14 '13 at 07:03
  • @assylias, oh got my mistake, i forgot to add one more parameter. let me update my answer again. – Raynold Mar 14 '13 at 07:16
  • @Raynold I put the TimerTask class in the activity class and called the timer constructor in the onCreate method, it is throwing an exception – CodeAddiction Mar 14 '13 at 07:18
  • I am not getting the timer to execute the task successfully – CodeAddiction Mar 14 '13 at 07:32
  • @CodeAddiction, try my new code, its working , i just created it. check my updated answer – Raynold Mar 14 '13 at 08:13
  • @Raynold, yes i tried but the application hangs up and the time and forecast i am getting from yahoo api is not updating :( – CodeAddiction Mar 14 '13 at 09:40
  • that means you may have slow internet connection. – Raynold Mar 14 '13 at 09:42
  • @Raynold, may be you are right because the TimerTask is calling the function perfectly after the defined time but the api is not called and it may be the issue at my end. thanks anyways for helping me to have a working code at least :) – CodeAddiction Mar 14 '13 at 09:47