Manage offline hour in Android - android

I want to do an Android app that submits data to a web server. This application needs to run offline and the hour of the submitted info is crutial. We can't rely on the hour provided by the client, so we are always setting it in the server side. But, when android app goes offline, we need to keep a private clock separated from the system hour (because it can be modified by the user). My question is how can i achieve this? The first solution that cames to my mind is to keep a private clock in our app, but this is going to crash when someone closes it or when shutdown occurs. There is something done to achieve this in Android? Also, we are going to use Ionic framework (suggestions accepted ;))
Thanks.

You can achieve that by using SystemClock.
It allow you to know the time elapsed from the System startup.
Store the server timestamp and wake up you're app with AlarmService. Then check if the elapsed time is correct.

Related

Mobile App: how to get correct time when offline

We're building an App using Ionic/cordova that will need to run on Android, iOS and Windows 10 Mobile.
The goal is to register start and stop events, we'll need the exact time and location when this events are registered.
There is a serious concern that the users will modify the timesettings on their phone to send false data.
As long as these events are sent immediately to the server, we can use the server time to validate what the user is sending us. But there is also a requirement to be able to work when offline.
Our first thought was to use the GPS time. In cordova the position object has a timestamp.
https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-geolocation/
But after further reading, here and here, that this isn't necessarily the time of the GPS server but also just the local time. I've read about work arounds using the uptime of the device, but this won't work as soon as there is a reboot. But most of these conversations are a few years old.
For now we think the simples approach will be to flag events that are sent offline as 'suspicious'.
Is there any other way to accurately determine the time when you're offline without risk that the user tampered with it?
Haven't tried it myself,but what about getting the time from the Network Provider, as said in this SO: I want to get time from GSM network
long networkTS = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER).getTime();
Time returned by getLastKnowLocation could be old if you want current
time use
locMan.requestSingleUpdate() and pass the network provider
Though, this may not work in iOS or Windows.

Android: How to detect if user changes system time without running a constant service to check time

In my app I want to allow user to be able to use it for 7 days offline usage for free, but want him to connect online once 7 days are done and buy a subscription after that. So basically, I want to lock him out after 7 days from install. The problem is user can very well change system time and trick the app. Also, I cannot use server as the authority as the app is supposed to also work completely offline, except for the time of purchasing subscription.
I can run a background service to keep track of any system time change but I want to avoid doing that.
Is there a straight forward solution which can work even if the device is totally offline for 7 days.
You can save the current time in shared preference and compare it every time you sample it. If you get an older sample the user changed the time backward.
Use remote clock , for even simple read the time of server from mobile in desired time gap and use those number for your app's clock
So this is how I am planning to solve this, though this is not unbreakable but will work for majority of users.
So while signing for trial I get network time and store it as start_time in preferences. Next on each app start and app resume I read the current_time and write to prefs. Once current_time of pref is greater than time of device, we lock the app. Once preferences are not there we ask user to come online as suggested by #Zach
To make preferences editing a little inconvenient to the rooted device guys I am storing data in encrypted form in prefs.
This is still no way unbreakable as one can decompile the apk and read the obfuscated code to guess the algorithm and the key but I guess the value of content in the app is not that high for someone to go through that much pain.
can add syscall and track the time offline, get it from here

Periodic background check for a very simple task (Android)

I'm currently making an app in Android that is checking an API which returns two things. Some text and a colour.
However I want this to be checked for updates every 15 minutes in the background and check every 5 seconds when the app is open. When running in the background it should give a notification if the status is changed.
Now I have checked numerous stackoverflow q&a's and forums, docs etc.. But I can't seem to find a good baseline for what I need. So many documentation that contradicts eachother.. I think that I need an Alarm Manager or a Service... but what do you guys suggest for my problem? The app may not harm the battery too much.
What I really would like to have is that the application doesn't have to "poll" the server every 15 minutes but that the application gets interrupted like.. "hey, there is a new status update". I can't imagine that messaging apps are constantly polling a server for updates? I haven't found much information about that topic... Any help is appreciated. Not asking for code but directions to get where I want to go.
Many thanks
If you're looking to poll the server every X seconds/minutes, AlarmManager(android guide, tutorial) is exactly what you need. However, as you point out this is probably not the best way to go about things. While the app is open you may want to look in to passing messages between the device and server via an open Web Socket. Once your app is closed you could, instead of the app polling the server, have the server push a notification, via GCM or some such, to the app when an update is available.
If you are doing both the server side project and the mobile application, You can use Any messaging service rather than polling for the server, Because there has to be a pusher implementation from the server side to push the status to the MS.
For now GMS is free, I hope it will remain the same :). Otherwise, You can use AlarmManager and IntentService to achieve your goal.

Schedule a task on iOS every 15-30 mins

I have an application that i have developed for ios, android and windows phone 8. The client now requires that the application checks if there is any data to be uploaded to the server every 15-30 minutes and if so, upload it. This is needed because the application is used in areas where there will not be an internet connection. The user adds data into the application and that data needs to be uploaded to the server once an internet connection is available
I have implemented this on android using the AlarmManager to schedule the application to check if there is any data and upload it, and on windows phone i used a PeriodicTask.
Is there such functionality in iOS? When i try googling this, i get many different things appear like NSURLSession, Background Transfer Service and others that say it ain't possible.
So, it is possible and what classes do i need to look into to get this working? If i have to aim at a particular iOS version then that is fine.
You can use NSTimer:
[NSTimer scheduledTimerWithTimeInterval: 1800.0 target:self selector:#selector(yourselector) userInfo:nil repeats: YES];
Apple documentation
No, this is not possible, well may be. Since only specific apps can run in the background like: AudioStreamer, VOIP, Accessory companion or location tracking.
These apps can't really schedule a task. Also it would be really bad for the battery to check some server every 15-30 min.
You can use NSURLSessions to create an uploading task that will run on the background.
You will have to setup the session correctly with all the delegate methods implemented: Background Transfer Considerations.
There are mainly 3 things to consider:
1.) Will the app remain active (visible on the device screen) for 15-20 mins. I suppose that won't be the case.
2.) When the app enters background state (not visible on the app screen, but is in the background), you can perform/schedule tasks for a maximum of 3 minutes.
3.) If the user kills the app (the app is not in background mode) then you cannot do anything.
NSTimer *yourTimer = [NSTimer scheduledTimerWithTimeInterval: 1800.0 target:self selector:#selector(YourMethod) userInfo:nil repeats: YES];
please try this ... this will work...
The upload task supports background operations by default (refer : https://developer.apple.com/reference/foundation/urlsessionuploadtask).
But that is limited to as long as the app is in memory. This is to ensure no app consumes battery without absolute NEED.
What you can do is upload in the background mode and check for pending upload data every time you launch your app.

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

Categories

Resources