Problem with timer

General topics about the Android-Platform itself.
Coding issues please to the subforum right below.

Problem with timer

Postby XBarboX » Mon Feb 14, 2011 9:16 pm

Hi,
i'm new from the android developer world.
I need some help for an application: this application just show the time (21:16:00)

I write this but work just one time, infact the task run just onw time and not go into an infinite loop

Code: Select all
package barbo.schooltime;

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

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import android.widget.TextView;


public class index extends Activity {   
    public static TextView ora;

   /** Called when the activity is first created. */
   @Override 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        ora = (TextView) findViewById(R.id.ora);
       
        final Handler handler = new Handler();
        final Date data = new Date();
       
        Timer t = new Timer();
       
        t.schedule(new TimerTask() {
                public void run() {
                        handler.post(new Runnable() {
                                public void run() {
                                index.ora.setText(data.getHours()+":"+data.getMinutes()+":"+data.getSeconds());
                                }
                        });
                }
        },1000);

    }
   
}


How i can do this? Thank you
XBarboX
Freshman
Freshman
 
Posts: 4
Joined: Mon Feb 14, 2011 9:11 pm

Top

Re: Problem with timer

Postby akarak » Tue Feb 15, 2011 12:36 am

Hi, XBarboX

XBarboX wrote:Hi,
...

I write this but work just one time, infact the task run just onw time and not go into an infinite loop

Because method "schedule(TimerTask task, long delay)" (according to the documentation for Android-SDK) schedule a task for single execution after a specified delay:

Code: Select all
public void schedule (TimerTask task, long delay)

Since: API Level 1
Schedule a task for single execution after a specified delay.
Parameters
task   the task to schedule.
delay   amount of time in milliseconds before execution.


XBarboX wrote:How i can do this? Thank you

Just use method "schedule(TimerTask task, long delay, long period)":

Code: Select all
void    schedule(TimerTask task, long delay, long period)
Schedule a task for repeated fixed-delay execution after a specific delay.
* Alexander Kalinovsky
User avatar
akarak
Junior Developer
Junior Developer
 
Posts: 19
Joined: Sat Feb 12, 2011 12:29 pm
Location: Minsk, Belarus

Re: Problem with timer

Postby XBarboX » Tue Feb 15, 2011 5:54 pm

I have change the code but don't work... why? I can't understand....
it show the time but it don't refresh the time....
Code: Select all
package barbo.schooltime;

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

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import android.widget.TextView;

public class index extends Activity {
   /** Called when the activity is first created. */
   @Override 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        final TextView ora = (TextView) findViewById(R.id.ora);
       
        final Handler handler = new Handler();
        final Date data = new Date();
       
        Timer t = new Timer();
       
        t.schedule(new TimerTask() {
                public void run() {
                        handler.post(new Runnable() {
                                public void run() {
                                ora.setText(data.getHours()+":"+data.getMinutes()+":"+data.getSeconds());
                                }
                        });
                }
        },1000,1000);

    }
   
}


p.s. ora is the textview where i want to show the current time
XBarboX
Freshman
Freshman
 
Posts: 4
Joined: Mon Feb 14, 2011 9:11 pm

Re: Problem with timer

Postby akarak » Tue Feb 15, 2011 8:38 pm

XBarboX wrote:I have change the code but don't work... why? I can't understand....
it show the time but it don't refresh the time....
p.s. ora is the textview where i want to show the current time


XBarboX, read and write you code more accurately :).
You create a data object only once, at the beginning of the method onCreate(). To refresh-time you must add a call "new Date" in the definition of method "run()" of the interface Runneable. Ie the code should look like this:

Code: Select all
...
t.schedule(new TimerTask() {
    public void run() {
        handler.post(new Runnable() {
           public void run() {
                Date data = new Date();
                ora.setText(data.getHours()+":"+data.getMinutes()+":"+data.getSeconds());
           }
       });
    }
},1000,1000);
...
* Alexander Kalinovsky
User avatar
akarak
Junior Developer
Junior Developer
 
Posts: 19
Joined: Sat Feb 12, 2011 12:29 pm
Location: Minsk, Belarus

Top

Return to General

Who is online

Users browsing this forum: No registered users and 2 guests