Android Question Once more, timers...

Discussion in 'Android Questions' started by Troberg, May 4, 2015.

  1. Troberg

    Troberg Well-Known Member Licensed User

    OK, here we go again: Timers.

    My problem:

    I want to show various bits of information on screen. Most of the time, none will be shown, and at most, maybe a handful, but there will be dozens of such items stored in a map that will occasionally be shown. Now, when I show them, I want them to stay a few seconds, then disappear, and I want some central handling of that, so that it won't have to be re-invented in every class.

    Now, I see two main ways of doing it.

    * Creating one timer for each class. Well, could be done, but hardly neat.

    * Set a "death time" on each item as it is shown. Then, using a single timer, check every 200 ms or so if any of them has passed their "death time". Only one timer, but iteration hundreds of items every second seems wasteful, and will make debugging a pain in the ass.

    Is there any good way of doing it? I'd like some kind of "Get back to me at this time" scheduler, though, of course, not something as Heavy as starting services or lobbing intents around. However, any solution that works better is good.

    Some pseudocode to try to explain what I'm doing:

    Sub Globals()
    Dim NfMap as Map
    End Sub

    Sub Setup()
    'Note: Different types
      Dim nf as NewsFlash1: nf.Initialize: AddNfToMap(nf)
    Dim nf as NewsFlash2: nf.Initialize: AddNfToMap(nf)
    Dim nf as NewsFlash3: nf.Initialize: AddNfToMap(nf)
    Dim nf as NewsFlash4: nf.Initialize: AddNfToMap(nf)
    Dim nf as NewsFlash5: nf.Initialize: AddNfToMap(nf)
    Dim nf as NewsFlash6: nf.Initialize: AddNfToMap(nf)
    Dim nf as NewsFlash7: nf.Initialize: AddNfToMap(nf)
    End Sub

    Sub AddNfToMap(nf as object)
      NfMap.Put(nf.Key, nf)
    End Sub

    Sub ShowInfo(Key as string, Message as string)
    dim nf as object= NfMap.Get(Key, Message)
    3000'Hide after 3000 ms
    End Sub

    'Something to hide nf after the time has passed???
    Edit: In other words, what I need is something like "Fire an event at these times", where I can add times dynamically.
    Last edited: May 4, 2015
  2. sorex

    sorex Expert Licensed User

    I wrote a tween class that works kind of what you want.

    The class has it's own timer and you feed it with objects from other classes.

    I just use a timer where I update the map elements or remove items if a certain condition is met.

    Edit: add 2 extra ints to your cutom types and store the current ticks in it and the delay.

    in you timer you check if abs(typetick-currentticks)>delaytick if so remove the element from the map and the panel.
    Last edited: May 4, 2015
    Troberg likes this.
  3. Troberg

    Troberg Well-Known Member Licensed User

    That's more or less my second solution. The main problem is that it'll have to fire events pretty often, which makes stepping through code when debugging a nightmare.
  4. JordiCP

    JordiCP Well-Known Member Licensed User

    To minimize the number of events and use only one timer, you can manage a DeathList with the active messages and their remaining time.

    Something like this
    type mItem(msg as string, remainingTime as long)

    Sub Show(msg as string ,timeToShow as long)
    Dim I as mItem
    'If the timer isn't active, activate it with that interval.

    sub Timer_Tick
    'stop the timer
      'for each item as mItem in Deathlist, Item.remainingTime -= Timer.Interval
      'remove those elements which have a remaining time <=0 and update the rest
      'if DeathList not empty, activate Timer with the minimum remainingTime
    end sub
    Troberg and DonManfred like this.
  5. Troberg

    Troberg Well-Known Member Licensed User

    Actually, I just did it using more or less exactly that method. Some intelligent disabling of the timer when the list is empty made it much neater.
    JordiCP likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice