Know the old time - android

I want to design app in which I can get the time before the user has changed to any new time.
I am using
android.intent.action.TIME_SET
To know that user has changed time
eg. Suppose current time is 10.00 pm User changed it to 09.00 pm So can i get previous time that is 10.00 pm

Question was asked some time ago, but I have generally the same problem and cannot find reasonable (efficient) solution.
Just to recall, I want to know the time that was set before user changed time. For example, user changed time from 6:12 PM to 3:21 PM. To the best of my knowledge, there is no information about previous time in android.intent.action.TIME_SET. But when handling this intent I want to somehow know that the time was set to 3:21 PM when there actually was 6:12 PM.(let assume the same day).
According to accepted answer to this question:
android detect user modifying device clock time
If I want to handle situations when network is not available, I have to kind of append my own timer to my application, measure time on my own, and when handling
android.intent.action.TIME_SET
get old time from this built-in timer.
But it seems to me, that this is quite heavy solution and I reckon there must be simpler way to do this.
So the question is, what is the best (most efficient and the most simple) way to handle situation described above. Despite this problem seems to me very common, I couldn't find fair solution.

You could try to monitor the difference between System.currentTimeMillis() and SystemClock.elapsedRealtime(). The value of System.currentTimeMillis() would be affected by the change of system time whereas the value of SystemClock.elapsedRealtime() would not.

Related

ACTION_TIME_CHANGED Intent, can we set this to trigger based on a "set time"?

Just a quick question about this, for the android platform, I was directed to this link from another question..
Is there a way to detect when the user has changed the clock time on their device?
If the player changes their system time, this event would trigger. I would only like this event to trigger if the time changed is greater than 40 seconds, is this possible?
Is there a way to make this trigger for certain times only? for example, 30-40 seconds is no big deal for me, however anything over that, I would like this to fire, and then check in with a server to see if the user has tampered with their device time?
I realize there will be other things to worry about such as timezones, I think I can work that out if it is possible for this to only fire if the time changed is greater than 40 seconds.
This is from the documentation.. suggesting that this is not possible. Can anybody clarify?
public static final String ACTION_TIME_CHANGED
Added in API level 1
Broadcast Action: The time was set.
Constant Value: "android.intent.action.TIME_SET"
No, that isn't possible. This broadcast is sent when the time is set (changed). If you are listening for this broadcast Intent you will get it every time. There isn't a way to tell Android that you only want to get triggered under certain circumstances.

Run a function on the first day of every month

I want to run a function from my Android application at a fixed time (let's say 8am) on the first day of every month. The function, depending on user's preferences, will either serve a notification or start downloading a file over the Internet (both of which are already taken care of) or do nothing.
I tried using BroadcastReceiver with action android.intent.action.DATE_CHANGED but I read that it is fired only when user changes the date manually (although even then it worked the first 2-3 times I tried and then stopped working). I think an AlarmManager will be able to do what I need done but not really able to figure out how to implement it.
This should run whether or not my application is active/running. What is the optimum way to do this? Any supporting code will be appreciated.
Just start a service when start up. In the service,you could check the date in another thread.
Use an alarm
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis()/*now*/, TIME_REPEATING, pendingIntent);
The TIME_REPEATING will be your calculate of the first day on the month, and the pendingIntent your service to run the task.
The unique hard work will be know how many time need to pass to the first day of the next month. But it's possible and very pausable to calculate.

Get the real time passed on Android

I'm developing an application, which will have a custom yearly subscription license. I need to know exactly how much time has passed. The user could keep the device offline, therefore I can't check the time through internet. The user could turn back the clock, therefore I can't be sure of really passed time. Is there a way to get the real time elapsed?
I think you can use System. nanoTime(), which can help you measure an absolute elapsed time (as opposed to System.currentMillis() which will be adjusted if the system clock is changed).
See the nanoTime and currentMillis javadocs for more information.
ps: I have not tested it.
you can have a preference or database that need to be stored the time when user install the application ... and you will always compare the time passed with the difference between stored time - current system time ........
or may be make a service to get network time

android: do something in certain time

I was read almost all article that have same question in Stackoverflow and somewhere else but those made me confuse.
my problem: I want my App toast something in certain time to the user (like alarm clock App that start ring in certain time) as an example, I want my App 2 days and 10 hour latter show a toast. but, during this period every thing maybe happen like application close, phone will restart or Etc... and the App doesn't show any thing.
my question is: How can do something in certain time in android App?
So now what is the solution? can any one help me and give me a sample code plz?
thank you in advanced,
What you want is an AlarmManager
You can also find a tutorial here. If you are scheduling a precise time, you might want to actually be conservative and wake yourself up a little early, then schedule a toast using a Timer for finer grained control.

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