Is there a way to get an alarm to fire some time after the user has last touched the device? i.e. when the device has been in the users pocket for a while, or when the user is sleeping.
I need to do a db-locking update for 2 minutes every few days, preferably when the user doesn't want to use the app.
Edit:
I have no clue when the user will not be using the app, since it's something that will be used anytime during the day, just before going to bed and just after waking up.
Currently I'm just picking a random time between 07:00 and 08:00 every 3 days (using AlarmManager, restarting on boot, etc), hoping that the user doesn't really need this feature at that time of day, but I'm pretty sure someone will.
Related
This Doze mode makes it quite hard for me to implement proper alarms system. Basically I want to allow my user to run a webservice call each 15 or 30 minutes for instance. So let's say the user sets the app to run the alarm each 30 minutes. While the device is normal use, the alarms will fire more or less exact (I'm using setRepeating) but it's good enough for my purpose.
When the device is dozing, as per docs, my repeating alarms do not fire, BUT when the device exits doze mode, all the alarms that did not run, run one after another(or similar). So I end up in the morning with maybe 4-6 alarms fired one after another, all fetching the same data from the webservice. Or same thing when a maintenance window occurs...
Is there a way to tell the doze mode that if my alarm did not fire at required time, to not run it at all? Or if there are multiple alarms that did not fire, only fire the last one?
LE: I had an error in code which ran the alarm too often, that is why I had the impression that postponed alarms will all run one after another...
My application should detect whether the user was sleeping to start itself with a welcome message when the user wakes up and powers up his phone for the first time in the morning, afternoon, or whenever he wakes up, by pressing the power putton and entering the pattern on the homescreen or pin or whatever.
I want to do this by measuring the time the device was used the last time. I presume 8 hours of sleep, so if 8 hours passed since the last switching off, the next time the mobile phone is being switched on, the application starts(respectively a function inside).
The screen going on because of an alarm or anything but user interaction should not count.
I looked at logcat and it gave me the useful information I/PowerManagerService: Going to sleep due to power button. So a PowerManagerServiceListener could be a way. I found https://developer.android.com/reference/android/os/PowerManager.html but it is for actively keeping the display on, not for passively reporting it.
Another candidate is KeyguardViewMediator: onStartedGoingToSleep(2) and KeyguardServiceDelegate: onScreenTurnedOff().
Sure, I could create a polling application, that looks through logcat, but that sounds like a strongly battery draining approach.
Which performance friendly way is there to accomplish what I want?
According to your description, you are interested in a granularity of hours. Then you can schedule a repeating alarm every hour (see Scheduling Repeating Alarms), save the time in a Shared Preference, receive a BOOT_COMPLETED broadcast, compare the times and decide what to do.
However, I guess a lot of users don't turn their devices off, so I would try to find out if the assumption your approach is based on is valid.
You can also try getDetectedActivity() from Snapshot Awareness API and check if DetectedActivity is for example STILL at some specified interval.
Hello i am relatively new to android so sorry for any mis-interpretations/confusions.
I am working on an application which triggers alarm on the selected time, it working fine, but there is a thing which makes me confuse.
When user selects alarm time, alarm is set for that time but if user changes the time of their android mobile phone, the alarm will be triggered on the time ie- (SystemClock.getTimeInMillis() + selectedTimeInMillis).
How to avoid this situation, i want to trigger the alarm on the exact selected time even if user changes the time of the mobile.
Any help is appreciated.
You could add a request to an internet time server like this:
How to get current time from internet in android
So it will always use the real time and not the system time. I don't think there is another way because you never know if the user changed the system time.
I'm developing an app that need to know the moment when the system startup, periodically.
I use System.currentTimeMillis(); to get the current "date" in millis.
I get the system elapsed real tine with SystemClock.elapsedRealTime().
I subtract the total time with the elapsed time, and I retrieve the system bootup time.
I would understand if the value change of milliseconds over time(different times in executing instructions). But for me it changes of seconds!!! Is very strange.
Someone have an idea of why?
Differences of seconds (or even tens of seconds) should not be surprising. "Current Time" corresponds to what the user perceives from the clock displayed. If you watch the displayed clock, it will not always change immediately. Read the docs:
http://developer.android.com/reference/android/os/SystemClock.html
It's pretty clear that you can't depend on currentTimeMillis for much of anything where you need millisecond accuracy. However, in most applications you really don't need that level of accuracy...
If you are trying to determine "exact boot time" from a specific moment in time later, a few seconds probably is the best you will do. Besides, what qualifies as "booted"?? When the kernel is loaded? When the user can interact with the device? When the device screen lights up? When the device can actually be unlocked?
You should probably just listen for RECEIVE_BOOT_COMPLETED (Android - Start service on boot).
Here is the Android doc: http://developer.android.com/reference/android/Manifest.permission.html#RECEIVE_BOOT_COMPLETED
I'm writing a simple Widget for Android which displays information which changes for every day. So the widget needs to be refreshed on midnight. The whole refreshing is implemented as a service and runs nicely, the problem is the invocation:
The only solution I found is to use the AlarmManager to a add an exact reoccurring timer on midnight each day. Then aquire a partial Wake-Lock, to make sure the device stays awake and run the code. This should work as expected but due to the usage of the wake lock, I am waking the device, so I am searching for a slimmer version:
There is no need to wake the device up on exact midnight, it is enough if I receive a timer event the first time the Device is up again on a new day. If the device is sleeping, nobody can look on the widget, so it is ok if the widget updates whenever the device is switched on again.
In other words: How do I run a service on the first moment of a day when the device is not sleeping, thus preventing a wakeup? I still need the device to stay awake than for period of time.
How can this bis done?
You can tell the AlarmManager to delay your invocation until the device wakes up anyway.
Then don't use a WakeLock in the.