Feasibility of perfectly syncing number counter between app and server? - android

First time asking a question. I appreciate any thoughts/feedback you may have.
Part of the functionality of an app I am working on requires that an internal counter in the app be in perfect sync with a counter on a remote server.
More specifically, there are 86,400 seconds in a day. Assume on a server I start a counter that ticks from 1 to 86,400 progressing at one tick a second. When it gets to 86,400 it starts over at 1. This then repeats indefinitely.
Part of the functionality of the app is the first time it is launched by the user it will connect to the server and sync a similar running counter within the app to perfectly match the counter on the server. (e.g. at the exact same time that the server counter ticks to perhaps 10,215.....so should the app counter be ticking to 10,215.)
Assume perhaps thousands of users have the app. I need it so that the thousands of individual app users now have 1 - 86,400 counters all running exactly in sync with each other. With the idea that a user only has to have their app perform this server sync a single time. After it's synced, the counters within the apps would then run in unison in perpetuity.
Questions:
1) Is this feasible to do? For the reasons I need this to occur, it literally cannot be off by even a second or two. They all have to match perfectly.
2) Would varying internet and/or phone speed by user be a problem (during the sync process)? Again, it is a one time sync. It does not have to sync on a continuous, real-time basis. Nonetheless, the first sync does have to be perfect.
3) If the answer to the above is that it is doable.....getting greedy here, can I sync it at even faster speeds? Instead of the counter just counting up one tick per second (i.e. 86,400 ticks per day), can the counter tick at perhaps a rate of 1/10 of a second (864,000 ticks per day). At that super fast tick speed, could the perfect sync still be accomplished?
While not the exact reason for needing the above, to give perspective.....imagine I had 100 people sitting in a room download the app. They then open the app which then all perfectly sync to the server so that they are all now running their individual counters in perfect unison. At that point, they lose internet connectivity (yet, the counter continues to run within the app in their phone.) If the app was programmed to start playing a particular song when the internal app counter reached tick X, all 100 phones would start playing the song at the exact same time (when their counters in unison reached tick X).
Sorry for the long question, but wanted to lay out my issue. Thanks everyone!

Related

Sync stopwatch runtime duration over a socket

Currently have stopwatches running on multiple devices that are kept in sync via a socket server. When a device starts the stopwatch it sends a message to the socket server, and the other clients listening see this and also start their stopwatch. There is not a huge amount of latency so it's not a big deal that while they are running the times are slightly off from each other.
However when "pause" or "stop" is pressed we want to have that final time shown on the pausing clients device to also be the same time shown on all devices.
Currently here is what happens when the timer is stopped/paused:
long time = Long.parseLong(message);
mChronometer.setBase(time);
The message being sent is from the code here:
this.sendWebRequestStopTimer(mChronometer.getBase())
While this does cause the time to be adjusted, it is slightly off usually by less than a second, typically 100-400 milliseconds. I assume this is because the each device has a slightly different time despite same NTP servers.
So in short, is there a better way to adjust the code so that the child devices will display the exact same time as the parent device (one who hit pause)?
Note: Any of the devices can play/pause/stop the stopwatch. No one device is in charge of controlling this stopwatch function. So device 1 could start the timer, device 2 could pause it, and device 3 could resume it.
Have the device that presses pause/stop send its time to the server, and echo that to all other devices. There's really no other way of doing it, short of a hard wired dedicated network. And even that would still leave you with some discrepancy in the 10s of ms range. Of course this could cause some of the devices to show a smaller number, looking like it went back in time. No real way to avoid that.

Using Android Tasker to override app settings

BACKGROUND:I'm in the process of writing a tasker application that tracks usage of other apps--it keeps track of the time that a reading app is open, for example.
In order to make sure that I'm actually reading and not just leaving the reading app open, I want to make the display timeout 30 seconds long.
ISSUE: The reading app locks the display so it never times out. How can I override the reading app settings to make my 30 second timeout take precedence?
I can't find anything out online because everyone is trying to do the opposite. However, everyone else's Tasker timeout seems to avoid conflicts naturally, so I'm not sure why mine is having issues.
Pseudo code:
Event: Reading app is active AND display is on
Start time = current time
Timeout save = system timeout
Display->display timeout->30 seconds
Event: Reading app closes OR display is off
Total time += current time - start time
Display->display timeout->Timeout save
have you tried with Secure Settings plugin?
it does have better than Tasker control over some of the OS parameters
After extensive research, I have found that there is no way to override the setting. (At least not for the beginners like me)
Google briefly made it possible to override app permissions in Android 4.3, but soon took that privilege away in a subsequent update. Pity.
A cheap and dirty workaround that I came up with is to have a small dialog in a corner with a timer that counts down for your screen timeout. In the last 10 seconds, a button becomes visible which resets the timer and hides the button, or else the display is shut off. (This works for the purposes of MY app -- My intention was to make sure the user was interacting with a third-party reading app, and not just turning it on and walking away, as my app counts the amount of time the reading app is open and the display is on).
#Tomaski - Unfortunately, the Secure Settings plugin doesn't have this power, although you are correct that its capabilities are greater than vanilla Tasker's.

Restricting Users from Gaining Life when changing local device time & date cocos2dx Game

Am developing a cocos2dx game(IOS & android) in Which we provide the user Diamond for every 4 hours.
Since no Server is used,every data are saved locally in CCUserDefault.My problem is ,now the user
able to Gain Diamonds by setting future date & time in local device. I am fully dependent on the
device time , as i can't run my own timer when the game is not in memory .I am stuck here
eg : candy crush saga we can get life when changing date or time.
please guide me in the right direction . Suggestions are welcomed !!
Thanks
You are using an information on which you have no control whatsoever : 'time of day'. You dont control the clock, nor its accuracy, nor its precision. Instead of 'time of day', use 'play time' as your measuring stick. Store a minutesCounter in a secure place, and increment it every minute of active play. When minutesCounter reaches 240, you have a winner, award the goodies, and reset the counter to zero. In you game loop, setup a timer that does this whenever the app is running (not in background).

Android Clock which runs when device off

I am creating an application, which will save the current time (with some delay eg. 2 hours) in file, when the user presses a button. Later on, the application will check if the time has passed and do some stuff...
So... I click button in application (time gets saved in file)... I quit application... shut-down phone... I turn it on after 1 hour, get back to application... and I will still have to wait 1 hour until the application will let me do "something"...
QUESTION:
Is there a clock that cannot be changed by the user and keeps running when the device is turned off? I'm currently using SystemClock.elapsedRealtime(), which works fine, because even if users change the time in settings, elapsedRealtime stays the same. The problem is if the device gets turned off, because at every boot elapsedRealtime starts with 0.
I cannot use server time because application will not be connected to Internet.
If there is no such clock, please suggest me another solution.
actualy, you have no chance to get "off" hardware clock data. hardware clocks was just on older phones in the new phones i think nobody need it so they dont build it in hardware. In the old phones there was "hardware" clock but in the new device is nothing like that i think. I did read something about that google want to make some framework or what to implement it. But there is no alarms what are able to start in off mode.
So i am sorry, but i think it is not possible right now..
You could store your time in a database as a DateTime value, indicating Year Day Month Hour Second Millisecond, then you could request for a service to start on boot and read that data creating an alarm that triggers in the remaining time. I would give you a code example, but i'm not really good at java programming so it may be useless, anyway goodluck and try to implement this.
You obviously need to save your data to non-volatile storage. When your app is paused/destroyed by the Android, you should take it as a threat and save your time values to the disks, and then when your app has started again your app should read the data you have written before and keep on running as it would normally.
Well when it comes to question how:
the simplest solution is to use SharedPreferences,
the more complicated and the more flexible one is SQLite Database,
for more data on Android storage I will suggest: Storage Options

Time since the app is open in android

Hello
In my android application i would like to get Time since when the app is opened.
Initially what i tried was getting the time when the app is loaded from the server and then taking the difference with the current time from the device.But by doing that if the user changes the time then i willnot be getting the actual time.
Its not posiible to hit the server again for the same.
Is there any way to achieve this in android?
Please share your valuable suggestions.
Thanks in advance:)
Try the "SystemClock" class, "uptimeMillis()" method.
Store the result in a variable when the app starts.
Echoing what I said for your other question, you first need to become familiar with the activity lifecycle and understand the novel meanings (almost meaninglessness) of common words like "open" and "start" in the life of an android app.
There isn't any way you can prevent the user from changing the system time - you just don't have the right to do that to users. Normally this should be a rare event, unless you do something that makes them want to, such as lock them out of a free version of your app after so many minutes. (However if the phone is on a mobile network, presumably the mobile network occasionally adjusts its time to correct for errors in the device's oscillator, or administrative time changes)
What you can do is check the system time on every entry point to your application. If it ever goes backwards, well... something is going on. If the clock has been set back, you could assume no time between the calls with the negative time difference and resume your time meter from there, at least keeping all the previous used time in your record.
It may be that there are cpu cycle counters which you could query and correlate to system time, but this may be highly device specific and may in fact be resettable. And it may get weird if the cpu frequency is demand throttled.
You might be able to set a countdown timer as a bound on the maximum possible time between entry points at which you could meter. I don't know if these work reliably across system time changes or not - ideally they would. Testing or reading the source will reveal.
Use elapsedRealtime in your onCreate() store it. More reliable.

Categories

Resources