Battery efficient android widget that updates once a minute - android

I know this is a possible duplicate, but i havent found a specific answer to this? (there are partial answers on how to force the widget to update every second but its not all i am after...)
I need to have a widget that will update once a minute.
How to make a battery&CPU&RAM efficient widget for this purpose? (The data will be fetched from the internet and parsed before showing it to the user(a small amount of data and processing to be done.))
Should i make a Service that runs on BOOT if the user has the widget on screen and have that service schedule an alarm manager that wakes up a service that updates the widget?
Also i would like to pause this service if the screen is locked or off to save battery...

That's exactly how I did something similar. I created a Service (IntentService if you don't need anything complicated) and then register it with the alarm manager.
long interval = intervalMinutes * 60 * 1000;
long firstTime = Calendar.getInstance().getTimeInMillis();
Intent i = new Intent(ctx, AlarmService.class);
// Use the widget ID to update only one widget at a time.
i.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id);
PendingIntent mAlarmSender = PendingIntent.getService(ctx, id, i, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) ctx.getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, firstTime, interval, mAlarmSender);
being AlarmService.class your Service or IntentService.
For boot event, you can create a BroadcastReceiver and manage a android.intent.action.BOOT_COMPLETED action where you can register the alarm.
You can register for that event in your AndroidManifest:
<receiver android:name=".BroadcastReceiverWidget" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

Related

Android Re-create service at given intervals using AlarmManager

I have read about this AlarmManager, but it's still a bit confusing to me.
So I have a Service in my app, which I want to run all the time (its a medicinal app, so its supposed to notify the user the whole time its being used), but even though its a Service, Android kills it from time to time, so I want to schedule it to be recreated in like 30mins interval, and then scheduled again. How can I do it?
For started services, there are two additional major modes of operation they can decide to run in, depending on the value they return from onStartCommand(): START_STICKY is used for services that are explicitly started and stopped as needed, while START_NOT_STICKY or START_REDELIVER_INTENT are used for services that should only remain running while processing any commands sent to them. See the linked documentation for more detail on the semantics.
http://developer.android.com/reference/android/app/Service.html
Study this link... for implementing HOW TO CREATE ALARM MANAGER
CREATE ALARM MANAGER CLASS
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
// Set the alarm to start at 8:30 a.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 30);
// setRepeating() lets you specify a precise custom interval--in this case,
//20 minutes.
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 20, alarmIntent);
ADD THIS TO MANIFEST
<receiver android:name=".SampleBootReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
Hope this will help you.

Re-enabling the Android Alarm

I am having one alarm that can be created by the user and can be disabled by the user.
When user disables the alarm, I simply cancel the alarm using AlarmManager and store the alarm time somewhere.
Now, when the user re-enable the alarm, I create the alarm and set the stored time in alarm. The problem is that when I recreate the alarm with the stored time, the onReceive() method of alarm broadcast getting called instantly.
I am setting the alarm as below:
alarmManager.setRepeating(AlarmManager.RTC, time, AlarmManager.INTERVAL_DAY,
PendingIntent.getBroadcast(
this, alarmUniqueCode, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
How I disable the alarm when the user clicks on disable:
sender = PendingIntent.getBroadcast(this, 1, intent, 0);
alarmManager.cancel(sender);
I have added the alarm receiver in manifest file like below:
<receiver
android:name="com.sign.android.myscheduler.app.AlarmReceiver"
android:enabled="true"
android:exported="true" >
</receiver>
One more question: When I disable the alarm, I call cancel method. Should I need to unregister the broadcast receiver too? If yes, then what if I have two different alarms then? How can I unregister the broadcast receiver for only one alarm?
For your problem, the alarm will be triggered immediately if you set the time in the past
From AlarmManager API documentation:
If the stated trigger time is in the past, the alarm will be triggered
immediately, with an alarm count depending on how far in the past the
trigger time is relative to the repeat interval.
In this case, you have to check first whether the stored time is in the past. If yes, then you have to add intervals until the time is in the future.
Try changing the code to:
long now = new Date().getTime();
while (time < now) {
time += AlarmManager.INTERVAL_DAY;
}
alarmManager.setRepeating(AlarmManager.RTC, time,
AlarmManager.INTERVAL_DAY, PendingIntent.getBroadcast(this,
alarmUniqueCode, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
Regarding your second question, I cannot really answer. However, based on my self-experience, I don't have any problems even if I leave the receiver on.

Alarm doesn't trigger after reboot

I have an alarm to reset a data connection say every 15 minutes. The problem is, once the phone is rebooted, the application gets killed and the alarm (service) doesn't trigger anymore.
(This is not a duplicate, the other similar questions on SO do not solve my problem.)
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver
android:name="com.sang.mobiledata.ResetBroadcastReceiver"
android:exported="false" >
<intent-filter>
<action android:name="com.sang.mobiledata.IntentAction.RECEIVE_RESETCONN_UPDATE" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Broadcast Receiver:
public void onReceive(Context context, Intent intent) {
// if(CONN_ACTION.equals(intent.getAction())) {
if (intent.getAction().equalsIgnoreCase(
"com.sang.mobiledata.IntentAction.RECEIVE_RESETCONN_UPDATE")) {
MainActivity objMain = new MainActivity();
objNetwork.setMobileDataEnabled(context, false);
objNetwork.setMobileDataEnabled(context, true);
}
if (intent.getAction().equalsIgnoreCase(
"android.intent.action.BOOT_COMPLETED")) {
// code to restart/resume/retain alarm
Code to fire alarm (on the onClick):
Intent myIntent = new Intent(
"com.sang.mobiledata.IntentAction.RECEIVE_RESETCONN_UPDATE");
myIntent.putExtra("FLAG_KEY", false);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, myIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) this
.getSystemService(Context.ALARM_SERVICE);
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
long interval = intHrs * 3600000 + intMins * 60000;
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), interval, pi);
long mins = interval / 60000;
Toast.makeText(
MainActivity.this,
"Data Connection will be reset every " + mins
+ " minute(s).", Toast.LENGTH_SHORT).show();
}
Any suggestions please?
Read the AlarmManager document, there it is written, that AlarmManager will not hold alarms after reboot.
As per the Android Developers,
.... Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.
And regarding BOOT_COMPLETED, they write:
... This is broadcast once, after the system has finished booting. It can be used to perform application-specific initialization, such as installing alarms. You must hold the RECEIVE_BOOT_COMPLETED permission in order to receive this broadcast.
As for permission is concerned, you have already registered that. Well what probably happening is, the service BOOT_COMPLETED is being used locally, with the application. The reboot of the mobile is system activity, which is not overridden to accomplish re-registry of the alarms being saved. But I am not sure. So, you need to do something when execution comes in hand of BOOT_COMPLETED.
Read Automatically starting Services in Android after booting, this might help you with it.
What I did was, I registered all the alarms and formed a database, using SQLite; On restart, I use to reset all the alarms. Well that worked for me. If you want to go with my process then,
Make a database for your alarms, and save them there. Write your application in such a way that, when the phone restarts, the AlarmManager resets all the alarms. This is how it works.
In Automatically starting Services in Android After booting again. It is written:
Also note that as of Android 3.0 the user needs to have started the application at least once before your application can receive android.intent.action.BOOT_COMPLETED events.

Resetting alarms on reboot

User can create different alarms. So it's up to user when to keep alarm and he can keep multiple alarms and I maintain all the scheduled alarms in a database and show to the user for further reference. Below is my code.
if("CREATEONCE".equals(strparam1))
{
am.set(AlarmManager.RTC_WAKEUP, l2, pi);// l2 is time in millis
}else if("CREATEREPEAT".equals(strparam1))
{
am.setRepeating(AlarmManager.RTC_WAKEUP, l2, 86400000 , pi); //l2 is time in millis
}
So this is the code which sets the alarms. User can set multiple alarms. For example he keeps an alarm for 7.00 am for once, 8.00 am for once and 9.00 am dialy. So, for once alarms the code goes to if block and for repeat daily, the code goes to else if block in the code.
If the above 3 alarms are set by user at 6.00 am. If he reboots his device immediately after setting the alarms, the entire alarms don't trigger.
So I have read many posts regarding this like post1, post2. They all just gave to use broadcast receiver to know that device is rebooted. After the broadcast receiver receives a hint that device is rebooted, do I need to repeat above code again by getting the info from sqlite database to make all the alarms work? If so, can someone help me the way to do that from the broadcast receiver? Code snippets are appreciated
Suppose if the user sets 50 alarms, wouldn't it be a long process to get the info of all the 50 alarms and set them again?
I don't know how you are storing your alarms. But I suggest it would suffice to set up a system level alarm for the earliest coming alarm. Then once that is triggered, set up the alarm again for the next soonest triggering alarm.
I suggest putting your AlarmSetting call in a service and then call it from a broadcast receiver.
public class AlarmResetReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
//your code to set up alarms
}
}
There are also other conditions upon which you want to set your alarms up again in your manifest
<receiver android:name=".receivers.AlarmResetReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.TIME_SET" />
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
<action android:name="android.intent.action.LOCALE_CHANGED" />
</intent-filter>
</receiver>

Android: how to schedule an alarmmanager broadcast event that will be called even if my application is closed?

My app needs to execute a specific task every hour. It does not matter if app is runing, suspended, or even closed.
When app is running or suspended, I can do it by just scheduling an AlarmManager broadcastreceiver. But when the application is closed, I have to call "unregisterReceiver" to not leak an intent, and app will never be wake up (or something) to process the task.
Then, the question is: how to schedule an alarmmanager task that I don't need to unregister, so it will be called even if my application is closed?
Use AlarmManager.setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation) for this. Set the type to AlarmManager.RTC_WAKEUP to make sure that the device is woken up if it is sleeping (if that is your requirement).
Something like this:
Intent intent = new Intent("com.foo.android.MY_TIMER");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
long now = System.currentTimeMillis();
long interval = 60 * 60 * 1000; // 1 hour
manager.setRepeating(AlarmManager.RTC_WAKEUP, now + interval, interval,
pendingIntent); // Schedule timer for one hour from now and every hour after that
You pass a PendingIntent to this method. You don't need to worry about leaking Intents.
Remember to turn the alarm off by calling AlarmManager.cancel() when you don't need it anymore.
Don't register a Receiver in code for this. Just add an <intent-filter> tag to the manifest entry for your BroadcastReceiver, like this:
<receiver android:name=".MyReceiver">
<intent-filter>
<action
android:name="com.foo.android.MY_TIMER"/>
</intent-filter>
</receiver>
You need to user an Android Component Called Service for this. From the service code you can schedule your Task using the AlarmManager with PendingIntent Class for every hours. As your AlarmManger is declared in the Service Components it doesn't require any GUI and will get execute in background, till you have battery in your device.

Categories

Resources