Run Command/Service on first occasion of day - android

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.

Related

Any way to prevent alarm from firing if it did not run because of doze mode?

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...

Updating widget frequently using alarm manager

I have a widget that I need to update frequently with new content only while the device is awake.
I will use an alarm manager for this and set alarm type to either ELAPSED_REALTIME or RTC, as suggested on "App Widgets" guidelines on android developer site.
The problem is that I need to update the widget every 5 seconds (probably configurable) while the screen is on.
Wherever I searched people say that 5 seconds is insane, but I haven't yet really understand if this is gonna be a problem if I update only when the device is awake. Is there a different approach I can take to this problem? How clock widgets do this?
While experimenting I realized that when the screen goes off the alarm still triggers.
It stops triggering only when the device goes into deep sleep, in which case 5 seconds are too few anyway for the device to have time to go into sleep.
So what I did is filter the SCREEN_ON broadcast and schedule the alarm every 5 seconds. Then filter SCREEN_OFF broadcast and cancel the alarm.

Android - Start an IntentService on a specific day/time each week

I'm working on an app that has an IntentService that shows a Notification with showtimes for a local venue. I want the IntentService to be called every Fri at 5am or at boot on Friday morning.
What would be the best way to do this?
Right now I'm thinking of having a parent IntentService that will start at each boot and check what day it is and run the child service accordingly, but I don't know how to deal with a scenario where the user doesn't reboot.
Also, how would I run just the IntentService at boot and not the entire GUI?
Thank you
You can use the AlarmManager and use the setRepeating() method to set an Alarm that goes off every week at the same time.
Your booting requirement is probably not worth the effort, as very very few people reboot their mobile devices often. I've personally gone months without turning my phone off.
However, you don't need a parent service. You can do the checking of the day when the child service first starts up, and have it kill itself if its not Friday.

Android service behavior is different in debug mode and release mode when use sleep button, why?

I am developing an application that uses a Service as Contdown. When the user starts the countdown from the activity and the activity goes in background after the sleep button is pressed, I am using this Service to continue the countdown. When the count is finished the Service shows a notification with ringtone.
I use wait() "to count" the time in the Service. The strange behavior occurs when I use the application on a real device, but in debug mode. When Eclipse debugger is attached, the Service works well; when I test the application on the device without Eclipse debugger attached, the Service doesn't show the notification when the countdown is finished, unless the sleep button is repressed and the monitor is activated - then the notification and the ringtone are activated.
Can anyone can explain what causes this strange behavior? Maybe the issue is is related to Wake lock or a similar construct?
I use wait() "to count" the time in Service.
That is poor programming practice. Time elapses even without your tying up RAM to do it.
Anyone can explaine why this strange behavior?
The device fell asleep. This is normal, and desirable, behavior, to conserve battery life. With the USB cable plugged in, the device does not need to fall asleep, and if you checked the appropriate option in Developer Options, the device specifically will not fall asleep while plugged in.
Maybe is connected with Wake lock
Please do not use a WakeLock to keep the device awake for you to watch the clock tick by. Please use AlarmManager to get control when the countdown period is over. You can use a _WAKEUP-style alarm to arrange to wake up the device, and your BroadcastReceiver that gets control at that point can "launch a notification and ringtone". As a bonus, you can get rid of your service, so that your app can be better behaved on the user's device.
wait() calls are not guaranteed to wait for the right amount of time if the device goes to sleep. You should use AlarmManager to trigger your countdown timer instead.

Questions about Alarm Manager behavior and wakelocks

I'm using alarm manager in my service to set non-waking alarms every 15 seconds to execute a certain task. I don't want to wake the phone up as the task isn't time critical, so i'm using the ELAPSED_REALTIME flag to set the alarm. Here's the code:
alarm.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), 15 * 1000, intentRecurringChecks);
What I'm noticing in my logs is that the task is getting executed every 15 seconds. Does this mean the phone is staying awake even though it's screen has been turned off for half an hour? Is there a way I can be sure my application's not the one waking up the phone?
I've search about this topic but I can't find a proper answer.
Thanks for your help.
First, you shouldn't use AlarmManager for such timeouts. This is explicitly mentioned in documentation (read the bold part). It's better to use Handler based timers in your case. Here is an example: Repeat a task with a time delay?.
Second, when device is connected via USB, its not going to deep sleep mode. You should disconnect your device wait a minute or two. Attach it back and analyze the logs.

Categories

Resources