Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm no Android expert, but I'm guessing that the reason why your AsyncTask gets cancelled is because the OS, being a battery powered OS, is deciding that a long running background task is a bad thing for battery consumption. </p> <p>Having your program run a background thread for years is going to be a big disappointment for your users who will wonder why their mobiles run out of juice within a couple of hours. I suggest finding another way of doing your time measurement. What's wrong with using the device's real time clock? </p> <p>I see little point in trying to update a GUI once every millisecond. The OS isn't refreshing the screen at that rate anyway, and no user on earth is going to notice anyway. Plump for once every 40ms at most. </p> <p>And then there's the matter of accuracy. There's no point trying to measure time with millisecond precision on a device like an Android mobile over periods of hours, never mind days or months. Left alone the clocks and oscillators will be wrong by several seconds a day. The best one will be the real time clock, but even that is going to be pretty poor (they always are). Android is probably doing an NTP updated a couple of times per day, so there will be brief periods in a day when the local clock is close to being accurate (but even then it won't be millisecond accurate). </p> <p>So even if you do manage to measure time with millisecond precision over months the answers you'll be displaying to the user are going to be wrong by several seconds. You'll be lucky to get within minutes of the actual elapsed time.</p> <p>If your goal is just to have a stopwatch display rapidly updating when the application is in the foreground, just loop reading the real time clock and calculate/display the time since the stopwatch was started. Don't bother doing anything in the background, just sleep. When your app becomes foreground again resume the loop; the device's real time clock will have been ticking away all the time you're app is asleep, allowing you to calculate and display the time difference. This will be a lot simpler than trying to have a long running background thread, and it will also be more accurate than any other way you might choose (though still not millisecond accurate).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COJust a note that there are indeed classes of applications that do this type of data collection all the time. Data acquisition for process control is one, machine automation is another, medical monitoring is yet another. Now this user's data may not need to be displayed and updated every milli, but they still may need the data to be (eventually) displayed with millsecond precision and accuracy. Just think, how would it look if you had to monitor a new type of medical pacemaker? It's just that kind of problem!
      singulars
    2. COWhat David says is true... I'm working on a project like the one he describes and this "stopwatch" is just like a test, but thats another story... The thing is, that it displays a text string like a common running stopwatch (with ms presicion), and it must be running as long as the user wants it to (even years)...
      singulars
    3. CO@arielnmz, ok, so it sounds like you're trying to do something that I strongly suspect Android isn't well suited for from the point of view of power consumption. Doing anything once every millisecond over a long period of time is going to take some juice. Fine if you've got a mains supply! I'm afraid I don't know enough about Android and timers, but Linux has got some precision timer capabilities. You can do short nanosleep()s that are actually sleeps rather than a busy loop. Apply the PREMPT_RT patch and you'd even stand a chance of that working reliably. Dunno about Android though.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload