Time in android - android

In my android application I would like to block the user from changing the device time when my application is opened or minimised. The user should be able to change only once the app gets closed.
Please share your valuable suggestions.
Thanks in advance :)

Android applications do not get closed or minimized, they have different semantics. Please read up on the activity life cycle.
Android applications do not get to block users from conducting other operations on the device.

I would like to know how long is my app open so i thought i would take device time and by taking the difference i would get the time.
There is an API that will tell you how much time has passed since the device was booted, regardless of what the device clock is set to:
SystemClock.elapsedRealtime() "returns milliseconds since boot, including time spent in sleep."

Related

Manage offline hour in 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.

Trace time spent on each app in Android?

Is it possible to run a background service in android which can store the time spent on any application in android and also if we can take timely screenshots with the use of that background service in android.
I mean here that we can know that user has spent this much amount of time on this app and also take screenshots after each 5-15 mins?
Is it possible?
Please suggest some solution here.
Edited
Ok Lets forget screenshots. How can we trace time spent on which application? Same as below app
https://play.google.com/store/apps/details?id=com.appuccino.frequencyfree
You can get running processes by calling.
ActivityManager.getRunningAppProcesses()
you can save an array or List and get the processes running in foreground one by one. Then for getting ElapsedTime you can call getElapsedCpuTime()

How to identify when the Android device was used last time

I am writing a piece of code in Android that will try to identify when was the last time the Android device was used by the user. By used I mean any operation performed. If the device is not used at all for lets say 1 full day I want to perform some action for it in my service which is running. So basically when the device is not getting used (idle state) that time I want to identify all this.
Is it possible in Android to achieve this? How can I proceed further for this requirement.?
Any pointers will be helpful to identify the device was not used for X days(or hours) and perform some action on it from idle state.
Thanks!
Please follow this tutorial for screen turn On/Off. Hope it will helpful to you.
How to identify when the Android device was used last time?
When first time screen will turn on then you can read system current time and save in shared preferences and from second time (system current time - read last holed value in shared pref) and convert these milliSec to time.

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