I am creating an app which should have an internal clock of about 12 hours. Once the clock reaches 0 a button will become enabled in the bottom menu bar. I would also like to display this clock as it is counting down.
I am not sure how I can implement this. I would have to run some kind of background task that keeps checking to see if 12 hours is over, probably using the Date object's getTime() function. What I'm stuck on is I don't know how to run this task in the background. How do I run an asynchronous task throughout the whole app?
Related
I am currently learning react right now. I am following a react tutorial for a stopwatch application where it just increments the time every second. The code perfectly works as it indeed increments the time every second. However, if I open a new browser tab, or even open a different application, it seems that react doesn't update the state, meaning, the timer stops incrementing the value. But if I visit the react application, it begins to increment (once again).
I literally watch the timer go to '00:00:10' and then click another tab and wait for about 10 seconds. When I go back to the react application, the timer is still at '00:00:11', instead of '00:00:20'.
Is there something I am missing? Why is react behaving this way? It is because of performance issues? I am pretty sure this is a nature of react but I can't seem to find an explanation anywhere else.
I am also not sure if I should provide the code for the stopwatch application as I am sure that it's not a bug, nor a problem relating to the code itself. Let me know if I need to include the code so that I can edit this question.
If your website isn't active (in an active browser tab) then the Browser doesn't execute your JavaScript code. It does that to save CPU time - people tend to have a lot of tabs open.
For a timer application you want to remember the timer start time (e.g. with new Date().getTime()) and then every second you calculate the difference to this start time. This way if the user leaves the tab and comes back then the timer is correct again after latest a second.
Another benefit is that your timer will be more accurate even if the browser tab remains focused all the time. The timer calls aren't exact so your code isn't called exactly every second, but only about every second and the errors sum up over time. If you have a start time available then these errors don't matter.
Edit: There is more to this topic, the best thing I could find quickly is this discussion How do browsers pause/change Javascript when tab or window is not active?
I'm working on an app using geo fences but I'm hitting some issues.
I've been testing on Android predominantly so far.
a) I set up the geo-fence and it triggers fine for enters and exit events.
However, after an extended period time (such as a few hours or overnight) the events stop until the app is opened again.
b) My other issue is the geo-fence exit seems to trigger even when I haven't moved at all, and definitely not outside of the radius (150 meters).
I've looked at https://www.codenameone.com/javadoc/com/codename1/location/Geofence.html, and my code is very similar to the example.
Are there any other build hints I need to make it more accurate and persist in the background?
Geofencing should only be added once, it's a very flaky API in the native OS especially when it comes to background behavior that breaks frequently with OS updates.
Use something like:
if(Preferences.get("AddedGeofence", false)) {
addGeofenceBinding();
Preferences.set("AddedGeofence", true);
}
And see if this works.
I create a ThreadPoolTimer to refresh my token(which will expire in 12 hours), and the timer is expected to be ticked in 12 hours. However, I put my UWP app in background over 12 hours, and then resume the app, the timer is not ticked even though the token is expired. Seems that when the app is background, the ThreadPoolTimer is also suspended. I tried DispatcherTimer, which is not suspended when the app is in background, however it's only available in UI thread. Is there any replacement Timer in UWP can meet my requirement?
This is normal behavior - when the app is suspended, all it's processes are being stopped - take a look at App's Lifecycle.
Your scenario - to run code in 12h interval, fits BackgroundTask with TimeTrigger. Take a look at MSDN and there is also a sample.
Once I've also written a blog post about running such task - maybe will help. Also take a look at this answer at SO.
I try to make a Windows Phone app which plays sounds to help you fall asleep. I use SoundEffect class so that I can mix multiple sound files. The certification requirements says
"An app can play media in the background while it is running even when
its primary function is not related to music or video. An app that
plays music, audio, or sound effects must meet the following
requirements:" "The SoundEffect class must not be used to play a
continuous background music track in an app."
So if the SoundEffect class plays continuos music under locked screen is ok, right?
I made a timer with DispatcherTimer class, so that the user can set the time when sounds stop, so the battery won't go dead. But the certification requirements says
"All apps that run under a locked screen must stop any UI updates,
active timers, and other non-critical processing when notified that
the screen is locked."
So I can't use that, because my app has to be able to run under locked screen. What can I do to stop the music playing after a time interval or at a set time?
and other non-critical processing
Turning off the music is a key feature of your app, and therefore is a critical processing.
That said, since the DispatcherTimer has a strong dependance on the UI, I don't know if it will run properly under the lock screen. You should use a classical timer instead, or even better, just a thread with a Thread.Sleep instruction.
For instance, if you must stop the music two hours after the screen is locked, start a thread in the Obscured event and make it sleep for two hours:
private void TurnOffMusic()
{
Thread.Sleep(1000 * 60 * 60 * 2); // Sleep for two hours
// Turn off the music
}
In my Windows 8 metro style app, I want to update tile in every second. Is it Possible in WinRt apps? Please give me a solution for this issue
The rate at which app tiles are refreshed is determined by the OS and cannot be influenced by the app. Background tasks are limited to running once every 15 minutes and the BadgeUpdater can only be scheduled every 30 minutes.
See http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.notifications.periodicupdaterecurrence.aspx for more information.
This might be closer to what you are expecting to do.
Here's a solution for every minute (not second):
Tile Update every minute