Is AlarmManager suitable for writing clock widget? - android

I want to write my own clock widget on Android. I am using my own icons to show the time (i.e., updating the ImageView(s) every second). I found a method using AlarmManager to handle the update and implemented it, but I have noticed that the app is using up my battery quickly, which I guess is because of the frequent update.
Is there a more efficient method to do the update?
Appreciate your help.
Thanks & Regards,
Patrick

Battery drain on a frequently updating widget can come from:
*keeping the device awake
--do you really need to wake up the device each time - choose RTC not RTC_WAKEUP
*processing a lot of code each time the widget updates
--do you run a lot of code in onReceive in your broadcast receiver? change your process strategy to do as little processing as possible, because updating every minute is a lot! for example, dont generate data that doesnt need to be updated.

Related

Implementing a battery widget with alarmservice?

This question got me to think of the way I was writing my battery widget.
ACTION_BATTERY_CHANGED firing like crazy
I'm concerned with the power usage. I saw how frequent ACTION_BATTERY_CHANGED gets fired. Even if you check if the battery level actually changed before you perform the update operation, it still looks a little expensive.
So, which of either listening to ACTION_BATTERY_CHANGED in service or setting alarmservice for like every 5min - 10min to update the widget sound more efficient?
The widget would just be switching ImageView source according to battery level and updating a TextView. Though might consider drawing the text into the image using Bitmap.
You should not care about ACTION_BATTERY_CHANGED events, just use Alarm to check the battery status every 5-10 minutes, that is pretty much enough for your purpose.
On the other hand, do you seriously think someone needs another battery widget in 2013 ?
That depends entirely on if the battery events are raised more frequently than the alarm.
If you're making an app widget make sure you're not updating when not visible.
I made a live wallpaper based on battery level and see no adverse affects of using the battery changed events

Do tasks when device wakes up

I'm have a widget that is being updated from a service. When the user then turn off the screen, the service stops and won't start to update the widget again when the user wake up the device. So on to my question, is there some way to "restart" the service when the user wake up the device? I know I can keep the service keep going when the screen is locked, but I think it would drain to much battery then... I have google'd alot but haven't found anything...
Thanks in advance!
EDIT: Okey, this is strange! Now somehow it does that automatically, so it's working now! Thanks anyway!
EDIT 2: Or, now I don't really think it's the screen lock that stops the service, cause it just sometimes stops randomly without crashdialog when I use the phone... Anyone know how I should do to fix that?
For a widget it usually doesn't make sense to keep a service running all the time, you usually update the widget periodically.
The easiest way to do this and to be battery efficient is to schedule an intent using AlarmManager and setAlarmInexactRepeating:
Schedule a repeating alarm that has inexact trigger time requirements; for example, an alarm that repeats every hour, but not necessarily at the top of every hour. These alarms are more power-efficient than the strict recurrences supplied by setRepeating(int, long, long, PendingIntent), since the system can adjust alarms' phase to cause them to fire simultaneously, avoiding waking the device from sleep more than necessary.
The intent should trigger a BroadcastReceiver which in turn should trigger your service to update the widget. Using IntentService is usually a good choice for that.
If you need more control over the intent scheduling, you can also set the alarms yourself in when the service is finished with updating the widget, and supply RTC or ELAPSED_REALTIME as the type of alarms. Both types won't wake up the device if it's sleeping. If the device is not sleeping (but locked) you'll effectively piggy back on other events that woke the device.
If you really need to update as soon as the device is unlocked, the ACTION_USER_PRESENT broadcast might be what you need, but I'd still recommend to implement that as an addition to scheduling a periodic update.
Yes this is Possible!
When the user turn off the screen You Have to handle BroadCast Receiver (http://developer.android.com/reference/android/content/BroadcastReceiver.html)
That handle to Stop service & the service stops and stop to update the widget,
when the user wake up the device you have to Receive Broadcast start to update the widget.
I think it would be The best idea to save more battery.
For More information Please Refer this Link

How to poll for current time in a Service?

I did a search before asking so please don't tell me to do that.
I'm relatively new to Android so I get confused easily.
I'm making a simple app that changes the ringer volume according to time. For this purpose, I know I need a service that keeps running in the bg and monitor time. However, I really don't know how to approach this. I have the Professional Android Application Development book but haven't found anything that helps in there.
My question:
How to constantly check time without destroying the battery
If my code is needed (as proof of me actually working on this), I'll post.
You don't need a service. Use the AlarmManager class. Its like an alarm clock and it exactly what you need for this type of app.
need a service that keeps running in the bg and monitor time
No. Actually that's not how to do it. Services on android are different than your normal windows service/unix daemon. They should do their job and then stop themself until they get started again - to save battery.
You should start your service at a certain point in time by using the AlarmManager, it sends the launch intent to run the service. When the service is finished doing what it's supposed to do (change the rintone volume here), use Service.stopSelf() to kill it.

Updating weather info, should I use alarm and/or a service?

I am having some design techniques about How shall I schedule a code to retrieve the weather info?
Should I use alarms to retrieve the weather each 10 minutes?
And do I need to run a service for this? Or just put the code in the Broadcastreceiver and start when the alarm fired?
This is a good question. Yes, you will need to put this code in either a service or broadcastreceiver because when and activity loses focus (meaning the user is using a different app or the phone is asleep) they pause and/or close. However, i have no experience with either Services or Broadcastrecievers, so that is as much as i can tell you.

Run a service in the background forever..? Android

I am doing a Battery Consuming research on the Android phone. I want to run a Battery Check every 10 min till the battery totally dies. I have been having problems to make it work.
At my first try, I use a timer in a service class, and schedule the battery check every 10 mins. But soon I found that the service got paused when the screen goes off.
Then I try to use AlarmService, I use a alarm call to wake my service every 10 mins and to check the battery level and save the data to a file on the sdcard. It works with the screen off. However, I only got data of 9 hours...it seems that the AlarmService stop at some point after several hours. I don't know why it is like this, has the system killed it for memory issues?
So my question is, has anyone written some service to run (like forever) in the background before? How do you do it and I'd appreciate very much for a sample code?
I am currently reading some posts saying that there's a partial wake lock I can use to keep the service alive.. is this the correct way to do it?
Thanks a lot and I hope I can get some useful answers here.
At my first try, I use a timer in a
service class, and schedule the
battery check every 10 mins. But soon
I found that the service got paused
when the screen goes off.
You probably did not hold a WakeLock, so the device fell asleep.
it seems that the AlarmService stop at
some point after several hours
I rather doubt it.
So my question is, has anyone written
some service to run (like forever) in
the background before?
It is not possible to create a service that will run forever. It should be possible to create a scheduled task via AlarmManager that will be invoked "forever".
I am currently reading some posts
saying that there's a partial wake
lock I can use to keep the service
alive.. is this the correct way to do
it?
I'm not sure what "it" is. But, if you want to keep the device awake -- whether for your first approach or just while you are doing work triggered by an AlarmManager -- you need to hold a WakeLock.
Here is a project that does almost exactly what you describe for the AlarmManager, minus checking the battery level, but using a WakefulIntentService to ensure the device stays awake. If you cannot get this code to run until the battery shuts down, join the cw-android Google Group and report your findings, and I'll take a look at it.
http://github.com/commonsguy/cw-advandroid/tree/master/SystemServices/Alarm/

Categories

Resources