1

I have made a service that displays a toast showing the current time . The toast should be displayed when the value of "minute" changes. What should i do ? Here is the code. Is it necessary to create a listener to listen to change of the minute variable? Please help me.

public class Servicedemo extends Service{ @Override public void onCreate() { super.onCreate(); Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show(); String str; Calendar c=Calendar.getInstance(); int hour=c.get(Calendar.HOUR); int min=c.get(Calendar.MINUTE); str=String.valueOf(hour)+":"+String.valueOf(min); Toast.makeText(this, str , Toast.LENGTH_SHORT).show(); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); Toast.makeText(this, "Service Stopped", Toast.LENGTH_SHORT).show(); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } } 

1 Answer 1

2

you need create one timertask and override run method, like :

 public class yourAsyncTimerTask extends TimerTask{ @Override public void run() { Calendar c=Calendar.getInstance(); int hour=c.get(Calendar.HOUR); int min=c.get(Calendar.MINUTE); String str = String.valueOf(hour)+":"+String.valueOf(min); Toast.makeText(this, str , Toast.LENGTH_SHORT).show(); } } 

and set your timer

Timer timer = new Timer(); timer.schedule(new yourAsyncTimerTask(), 1 * 60 * 1000, 1 * 60 * 1000); 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you , the timer concept is very good. Now if i want to update it at the strike of every hour then what should i do? i mean at 1:00 , 2:00 , 3:00 and so on.
start_time = "time left until the exact time" timer.schedule(new yourAsyncTimerTask(), start_time, 60 * 60 * 1000);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.