Android Question How to set a timer /sleep interval based on device speed

Discussion in 'Android Questions' started by somed3v3loper, Jul 25, 2019.

  1. somed3v3loper

    somed3v3loper Well-Known Member Licensed User

    Hello guys,

    I have an application that starts another application , waits for a few seconds and then call a function but the problem is that devices speeds are not the same
    Is there a way that I make these seconds a variable based on device speed?

  2. DonManfred

    DonManfred Expert Licensed User

    your app too?
    call a function in your app? in the other app?
    You can not expect every device running the same speed.

    Adapt the time you wait. Make it more seconds until it works for all devices.

    If you call your own app then you can use intents to communicate between the apps. This is probably the best solution as app A gets an intent if App B finishes his work.
    somed3v3loper likes this.
  3. somed3v3loper

    somed3v3loper Well-Known Member Licensed User


    I already went with adapting but I thought there was a smarter way :D
  4. JohnC

    JohnC Well-Known Member Licensed User

    What you could also do is:

    1) Have a variable called "FINISHED" and set it to "False"
    2) Setup a timer for say 100 milliseconds. And when this timer triggers, it will set that variable for TRUE
    3) Then run a do-while loop and have another variable "S" that counts up:

    S = 0
        S = S + 
    Loop While Finished = False
    This way after the timer ticks, the variable S will have a number in it that is proportional to the speed of the device.
    somed3v3loper likes this.
  5. Didier9

    Didier9 Active Member Licensed User

    The issue is that speed is not a constant even for a single device as it depends what else it is doing, if memory is available, if some task is clogging the CPU, etc... So if I were to do this, either I would try to set a flag in the other app if you can, or just wait long enough, or allow the function to fail gracefully if the other app is not up and try again a littler while later.
    somed3v3loper likes this.
  6. somed3v3loper

    somed3v3loper Well-Known Member Licensed User

    That's right
    The problem is that I don't have access to the other application and as the customer has decided to give it a static value , I will stop here :D
  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