I want to know if the user is away for some amount of time. (and then do something according to it.) I cannot depent on screen sleep because user may have disabled it or its value may be greater than timeout of my application.
is there any information like (last user response time ) in android operation system ? and how can i get it?
last user response time = the last interraction time between the user and the phone.
p.s.:i am targetting android 1.6
No, there is no way to tell when was the last time the user interacted with your application in the SDK. You'll have to do something by yourself.
High level explanation of a simple solution:
Assuming you use some base activity inherited by all activities, you can log the current time in your BaseActivity.onPause method. Save it in the app preferences or in a database. When your initial activity starts (onResume) read the same value and compare it the current time.
You can also use Activity.onUserInteraction but saving to preferences/database everytime this is called will considerably slow down your app / feel less responsive!
Related
I am trying to introduce auto-save functionality on one of my Android applications. The idea is that as the user inputs first name, last name and after a fixed interval I would like to save that information to the server before the user hits Next button. The final goal is to have something similar to the draft option in the Gmail app where your email information is automatically saved. So, if there is a timer that runs every 10 seconds, I will pass the information on the screen to the ViewModel and let it deal with the logic of saving the data to the server.
A couple of options I have explored are.
Execute recurring code with a specified interval using Handler.
PeriodicWorkRequest -- however this option has a minimum interval of 15 minutes which is a little too much for my use case.
AlarmManager -- This option runs even if your application is not currently running, In my opinion, this option can be an overkill.
I wanted to know if there are best practices/blogs around this and if anyone I on the wrong path or potential red flags with this approach.
you can make countdown for 10 second, when countdown is down save the data and call the countdown again.
when your activity is destroyed, so stop the countdown
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
I wanted add application signature for some application, which will be valid only for 1 or 2 days. I did enough googling but did not find enough info. So Please let me know how can i make a application get expired in 2 days..
simply add an alarm of calculating time equals to 2 days at the very first start of the app.When the alarm gets expired you will get the callback and set a global flag as false.Code in your app that if that flag is false, display a lock screen
You could save the first start time in SharedPreferences and then on each start compare the current time with the saved one. If the 2 days have passed, you can do what ever action you want.
It will probably not be possible to automatically remove the app from the device though.
If your app sends data to a server, you should store the "first run time" there. Storing it locally on the phone (for example SharedPreferences) is not very safe as it's easily overcome by changing the phone's time settings.
My app will have a 'clock in' in the listview. As a user will click that item it will grab the time/date from the phone itself and send that data out to the server. I prefer doing this over using server time since if they dont have a signal/reception they wont be able to clock in. I would like to add a password security to the time/date settings itself so the user wont be able to take advantage of changing the time when clocking in. How can I make that happen?
Thanks
I don't think you can do that, but you can cross-check the time.
When you "clock in" also open /proc/uptime as a text file and read the value there. I believe it is in seconds. When you clock out, re-read the value and use the difference as a cross-check. If a server is also available, check the time from the server too (or report the clocking in immediately)
If the phone crashes or is powered off in between, the difference in uptime could be less than what you've recorded via the ordinary clock. In that case, the difference in uptime might be less than the ordinary clock (likely it will be negative) so if your clock-in was done without access to network time your software may have to have a way to report that particular result as unverified, and track the number of unverifieds per user to flag for human review if it becomes excessive.
A user could conceivably compile and install a kernel that lies about uptime, but that person could probably get around most of the other things you would do, too.
I would suggest to just send message to server "user X wants to clock in". And server will use its local time for "clocking in". This way you will completely ignore device's time, and have more control over your infrastructure.
In general, if you want something to be as secure as possible, don't do this on the client side (unless you absolutely have to). And in this case user may gain root on his device and use some command line magic to fool you server with fake date/time. Its not that hard. And you just won't be able to predict all the smart workarounds of you "time protection".
You could set up a service to run every minute (or so) that checks the time. If the time is not ~1 minute after the last check then it may have been changed. You should confirm with the server at this point to make sure the discrepancy wasn't caused by rebooting the phone.
If you find that the time was changed, you can change it back or log this with your main application and flag the "user" for disciplinary actions.
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.