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()
Related
I know that similar questions have been posted in the past and the most current solution I have found is to use a JobScheduler + wakelock + Foreground Service as explained for example in this excellent article by Roberto Huertas (https://robertohuertas.com/2019/06/29/android_foreground_services/).
However my doubt is to know if there is a limit for this method. Does it really work that well? What if the App stays in the background for days or even weeks, will it still work?
If the answer to these last questions is no, is it possible to keep a background service on Android > 10 that can keep running for days without stopping?
EDIT 1:
I'm trying to create a real time GPS tracking app (In this case I'm using Firebase). The company and the users who use it, give their consent to be tracked all day long during their activity. This tracking can be stopped if the user disables the option inside the APP.
I have managed to keep the service running in the background using various techniques, but after a few hours Android kills it.
No, it's not. It's actually less possible than ever. Background services are now limited to 2 minutes after you exit the foreground. Foreground services will be kept around for a while, but they won't stick around forever.
The correct answer on Android is to find a way NOT to need a service running at all times. This is almost always possible, but methods differ depending on what you actually need to do, which you haven't given us any info on.
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
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.
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."
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.