What Handler?

Put your problem here if it does not fit any of the other categories.

What Handler?

Postby phyburn » Mon Nov 03, 2008 3:51 am

I'm trying to run a class method I made every second and have it display text to the application screen. But I'm unsure on how to run the method every second. I can't find a good method in android.os.Handler or at least one I've gotten to work.

Any idea on what method I could use?
phyburn
Freshman
Freshman
 
Posts: 6
Joined: Sun Nov 02, 2008 2:26 pm

Top

Postby Quetzalcoatl » Mon Nov 03, 2008 4:15 am

Android Handler

Code: Select all
postDelayed(Runnable r, long delayMillis)
   Causes the Runnable r to be added to the message queue, to be run after the specified amount of time elapses.


I used it once a while back but don't have access to the code anymore :(

You pass in your Runnable derived class as 'r' and when the 'delayMillis' has expired it will kick off the 'run()' method in your Runnable derived class.
Image
Quetzalcoatl
Senior Developer
Senior Developer
 
Posts: 129
Joined: Sat Oct 18, 2008 8:21 pm
Location: near Manchester, UK

Postby phyburn » Mon Nov 03, 2008 4:40 am

cool thanks, but how would I run this every second? Isn't that just running it once?

Also would 1000 mean it would wait one second before running the runnable derived class?
phyburn
Freshman
Freshman
 
Posts: 6
Joined: Sun Nov 02, 2008 2:26 pm

Postby Quetzalcoatl » Mon Nov 03, 2008 4:48 am

You could have a separate method that does everything you want (every second) such as:

Code: Select all
public doPeriodicEvent(~~~~) {
     TextView.setText(~~~~~);
     //take dog for a walk etc etc

     //Kick off delay timer for 1 second
    if(runTimerForSomeReason == true) {
     myHandler.postDelayed(myRunnable, 1000);
    }
}


Then you could just call this from the point you want to start it from within your program and it will run until the if statement fails. In my basic example above, if runTimerForSomeReason equals true then it will start all over again otherwise it will drop out.

Yep, 1000 milliseconds = 1 second :wink:
Image
Quetzalcoatl
Senior Developer
Senior Developer
 
Posts: 129
Joined: Sat Oct 18, 2008 8:21 pm
Location: near Manchester, UK

Postby phyburn » Mon Nov 03, 2008 4:54 am

Quetzalcoatl wrote:You could have a separate method that does everything you want (every second) such as:

Code: Select all
public doPeriodicEvent(~~~~) {
     TextView.setText(~~~~~);
     //take dog for a walk etc etc

     //Kick off delay timer for 1 second
    if(runTimerForSomeReason == true) {
     myHandler.postDelayed(myRunnable, 1000);
    }
}


Then you could just call this from the point you want to start it from within your program and it will run until the if statement fails. In my basic example above, if runTimerForSomeReason equals true then it will start all over again otherwise it will drop out.

Yep, 1000 milliseconds = 1 second :wink:


I'm making quite a few clocks and would I be able to do this if I wanted lets say 10 different clocks all running at once? Wouldn't the application just stop at the first clock? Or am I missing something :oops:
phyburn
Freshman
Freshman
 
Posts: 6
Joined: Sun Nov 02, 2008 2:26 pm

Postby Quetzalcoatl » Mon Nov 03, 2008 5:03 am

Bah, now you're just moving the goal posts :P :D

I only used it to update something minor so I couldn't really tell you, I've got a feeling that it may block the thread so yes it would hold up at that point. Might be worth a try with 2 timers to see what happens, they are stored in a MessageQueue however so maybe it will act on each runnable in the order they come from the MessageQueue.

Do you really need 10 timers? or do they all just do something after 1 second? If they all just do something after 1 seconds then it might be worth doing all ten actions after the single runnable delay.
Image
Quetzalcoatl
Senior Developer
Senior Developer
 
Posts: 129
Joined: Sat Oct 18, 2008 8:21 pm
Location: near Manchester, UK

Top

Postby phyburn » Mon Nov 03, 2008 5:28 am

Quetzalcoatl wrote:Bah, now you're just moving the goal posts :P :D

I only used it to update something minor so I couldn't really tell you, I've got a feeling that it may block the thread so yes it would hold up at that point. Might be worth a try with 2 timers to see what happens, they are stored in a MessageQueue however so maybe it will act on each runnable in the order they come from the MessageQueue.

Do you really need 10 timers? or do they all just do something after 1 second? If they all just do something after 1 seconds then it might be worth doing all ten actions after the single runnable delay.


I need a lot of different clocks and count downs, but yeah I'm not worrying about any of that till I can at least get the time down lol

Okay I tried doing: Handler hand = new Handler();
hand.postDelayed(clock(), 1000);

and it would display the correct time, but I couldn't get it to ever update. Even if I made an if statement
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. if(true){
  3.  
  4.  Handler hand = new Handler();
  5.  
  6.         hand.postDelayed(clock(), 1000);
  7.  
  8. }
Parsed in 0.029 seconds, using GeSHi 1.0.8.4


it still wouldn't ever update the time =(

I'm going to try a timer and see if that works
phyburn
Freshman
Freshman
 
Posts: 6
Joined: Sun Nov 02, 2008 2:26 pm

Postby Quetzalcoatl » Mon Nov 03, 2008 11:11 am

If you put a breakpoint in your clock() method does the debugger break into there every second? It might be an issue with the drawing rather than the timing.
Image
Quetzalcoatl
Senior Developer
Senior Developer
 
Posts: 129
Joined: Sat Oct 18, 2008 8:21 pm
Location: near Manchester, UK

Postby phyburn » Mon Nov 03, 2008 11:14 am

Quetzalcoatl wrote:If you put a breakpoint in your clock() method does the debugger break into there every second? It might be an issue with the drawing rather than the timing.


I actually figured everything out earlier tonight I used a handler and looped it forever and I was able to get the clock running =)
phyburn
Freshman
Freshman
 
Posts: 6
Joined: Sun Nov 02, 2008 2:26 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 22 guests