Re: [android-developers] which is best Handler or Timer ?

2012-03-24 Thread Indicator Veritatis
This answer is true and correct, but could have used a little more 
explanation. The Timer class is included because it is expected in Java. 
But it is not particularly suitable for the way tasks and processes should 
be handled on an Android phone. Handler is. The OP can define an arbitrary 
message and get the effect of a timer by posting the message to the 
pre-existing queue with sendMessageDelayed(arbitraryMessage, 
timerInterval). Or if more convenient, instead of a message, put the 
Runnable you want executed on the queue with postDelayed(Runnable). The 
choice of which is best depends on your application's individual 
circumstances, but I prefer the message approach, unless doing a splash 
screen (for which see 
http://stackoverflow.com/questions/5486789/how-do-i-make-a-splash-screen-in-android).

Now for 'suitable', what I mean is: if you (the OP) use the Message 
approach, you do not have to define your own thread for the Timer, so you 
sidestep all the issues of thread management, letting the system handle it. 

See the class reference for Handler for details.

On Friday, March 23, 2012 1:33:19 PM UTC-7, Dianne Hackborn wrote:

 Handler.

 Just don't use Timer, Handler is the Android-centric mechanism.

 On Thu, Mar 22, 2012 at 10:53 PM, Perumss Naren peru2...@gmail.comwrote:

 Hi ,

 which way is best in performance is Handler or timer i need 
 to update textview as timer every second need to update time so  which one 
 will be best please tell the reason. because i need the reason thanks in 
 advance

 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(​savedInstanceState);
 setContentView(R.layout.main);
 
 timer = (TextView) findViewById(R.id.timer);

// handler.post(mRunnable);
 task = new UpdateTimeTask();
 handler.post(task);
 }
  

 private Runnable mRunnable = new Runnable() {
 @Override
 public void run() {
 timer.setText(dateFormat.​format(new Date()));
 handler.postDelayed(mRunnable, 1000);
 }
 };

 class UpdateTimeTask extends TimerTask {
 public void run() {
 timer.setText(dateFormat.​format(new Date()));
 handler.postDelayed(task, 1000);
 }
 }


 -- 
 Regards,

 Perumal.N

  -- 
 You received this message because you are subscribed to the Google
 Groups Android Developers group.
 To post to this group, send email to 
 android-developers@​googlegroups.comandroid-developers@googlegroups.com
 To unsubscribe from this group, send email to
 android-developers+​unsubscr...@googlegroups.comandroid-developers%2bunsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/​group/android-developers?hl=enhttp://groups.google.com/group/android-developers?hl=en




 -- 
 Dianne Hackborn
 Android framework engineer
 hack...@android.com

 Note: please don't send private questions to me, as I don't have time to 
 provide private support, and so won't reply to such e-mails.  All such 
 questions should be posted on public forums, where I and others can see and 
 answer them.

 

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Re: [android-developers] which is best Handler or Timer ?

2012-03-23 Thread TreKing
On Fri, Mar 23, 2012 at 12:53 AM, Perumss Naren peru2...@gmail.com wrote:

 which way is best in performance is Handler or timer i need to update
 textview as timer every second need to update time so  which one will be
 best please tell the reason. because i need the reason thanks in advance


For a resolution of 1 second, it probably makes no difference.

-
TreKing http://sites.google.com/site/rezmobileapps/treking - Chicago
transit tracking app for Android-powered devices

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Re: [android-developers] which is best Handler or Timer ?

2012-03-23 Thread Dianne Hackborn
Handler.

Just don't use Timer, Handler is the Android-centric mechanism.

On Thu, Mar 22, 2012 at 10:53 PM, Perumss Naren peru2...@gmail.com wrote:

 Hi ,

 which way is best in performance is Handler or timer i need
 to update textview as timer every second need to update time so  which one
 will be best please tell the reason. because i need the reason thanks in
 advance

 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);

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

// handler.post(mRunnable);
 task = new UpdateTimeTask();
 handler.post(task);
 }


 private Runnable mRunnable = new Runnable() {
 @Override
 public void run() {
 timer.setText(dateFormat.format(new Date()));
 handler.postDelayed(mRunnable, 1000);
 }
 };

 class UpdateTimeTask extends TimerTask {
 public void run() {
 timer.setText(dateFormat.format(new Date()));
 handler.postDelayed(task, 1000);
 }
 }


 --
 Regards,

 Perumal.N

  --
 You received this message because you are subscribed to the Google
 Groups Android Developers group.
 To post to this group, send email to android-developers@googlegroups.com
 To unsubscribe from this group, send email to
 android-developers+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/android-developers?hl=en




-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

[android-developers] which is best Handler or Timer ?

2012-03-22 Thread Perumss Naren
Hi ,

which way is best in performance is Handler or timer i need to
update textview as timer every second need to update time so  which one
will be best please tell the reason. because i need the reason thanks in
advance

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

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

   // handler.post(mRunnable);
task = new UpdateTimeTask();
handler.post(task);
}


private Runnable mRunnable = new Runnable() {
@Override
public void run() {
timer.setText(dateFormat.format(new Date()));
handler.postDelayed(mRunnable, 1000);
}
};

class UpdateTimeTask extends TimerTask {
public void run() {
timer.setText(dateFormat.format(new Date()));
handler.postDelayed(task, 1000);
}
}


-- 
Regards,

Perumal.N

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en