How to set time of choice in a Calendar object? - android

Sorry if the question is a bit misleading.
I asked another question earlier and by its help I got this far. Here is the problem.
I'm supposed to launch a Service at a particular time. Now... the service starts if I launch it instantly by using current time from a Calendar object, but when I set the time manually, the service doesn't launch. Here's the code:
Calendar myCal = Calendar.getInstance();
myCal.set(Calendar.HOUR_OF_DAY, 21);
myCal.set(Calendar.MINUTE, 4);
// Will use putExtras here to give service the details of which profile to activate
Intent intent = new Intent(Create.this, AutoBot.class);
PendingIntent pintent = PendingIntent.getService(Create.this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, myCal.getTimeInMillis(), 5*1000, pintent);
Now this should technically work, but its not. And that is why I need your help :(
Also, If I have multiple triggers (alarms per se), what would the third argument of setRepeating be?

I think you need to set up day month and year as well. Date in millis is from 1 Jan 1970

I think this link may answer your question. It explains a there is a limitation in
the system where the wake lock the alarm manager holds will be released too early when
delivering to a service. See this discussion: http://groups.google.com/group/android-developers/browse_thread/thread/2b98206d18a0192f/
Hope this helps.

Related

Developing reminder application in Android

I'm creating a reminder application that will show notifications on scheduled time, I'll be storing reminder related data in a database and created the working database and content provider for it, initially I thought of creating an alarm whenever user creates a new reminder, but now I'm not able to fire the notification whenever the alarm triggers, and don't know how can I retrieve the reminder related info from the database, and for repeating reminders like daily or weekly I want to reschedule the same alarm after it triggers as the android developer docs suggested so, but don't have the exact idea of it. Please guide me through this which approach will be better and is there any other better way to do it, and any working demo will be very helpful.
This seems like it is quite a few questions bundled up into one. I would suggest searching for each individual piece as all of the answers to your questions are on this site.
In any case, I copied over an answer on how to set a repeating alarm
AlarmManager am = (AlarmManager) ct.getSystemService(Context.ALARM_SERVICE);
Intent intent1 = new Intent(CurrentActivity.this, Alarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(ct, 0,intent1, PendingIntent.FLAG_CANCEL_CURRENT);
Date curr=new Date();
curr.setHours(h);
curr.setMinutes(m);
c.setTime(curr);
c.set(Calendar.SECOND, 0);
Calendar c1 = Calendar.getInstance();
am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
With this code an intent will be fired every day that calls Alarm.class. It is up to you to figure out if you need a service, intent, or anything else as Alarm.class, as it is unclear what the alarm needs to do based on your description.

How to test Alarm Manager which set for a INTERVAL_DAY

I written a small application which invokes a service using AlarmManager with interval of the day. To test this app, i am using emulator 5.1 . When i set manually date and time(for current date or future date time) the AlarmManager invokes service for that day. When i am trying to change the date for checking whether it will work for other day by changing date then its not invoking service.
Here is my code.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(calendar.HOUR_OF_DAY,13);
calendar.set(calendar.MINUTE,20);
calendar.set(Calendar.SECOND,10);
alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this,BackgroundService.class);
pendingIntent = PendingIntent.getService(this, 0, intent, 0);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
BackgroundService is service which popup with a Toast .
I have 2 issues.
How can test AlarmManager , which we set with Interval of a day?
The Alarm Manager is not exactly invoking at the second. For example , i set the alarm at 13:20:10 but its coming at 13:20:some Random second
Your help is much appreciated.
Thank you.
RTC (Real Time Clock) is the clock that you can adjust, so you could possibly just change the current time programatically for a test case. See this answer
You're using a alarmManager.setInexactRepeating method, which doesn't guarantee an exact time of alarm delivery. For more precise timings, use alarmManager.setRepeating, however keep in mind that it's not really recommended, as most use cases do not require exact timings and it will cause higher battery usage. See inexactRepeating and repeating

Calendar instance in android for AlarmManager

I am developing an app which displays notifications by using AlarmManager.
For that I'm taking the user input values for hour, minute and second.
Something like:
int hour = 4;
int min = 40;
int sec =36
Calendar Calendar_Object = Calendar.getInstance();
Calendar_Object.set(Calendar.HOUR, hour);
Calendar_Object.set(Calendar.MINUTE, min);
Calendar_Object.set(Calendar.SECOND, sec);
Intent myIntent = new Intent(MyView.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MyView.this,0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, Calendar_Object.getTimeInMillis(), myIntent);
Notifications and the rest of the code work fine, but the problem is that instead of 4:40:36 the notifications get invoked on the current time(as soon as i run/debug the app).
I think there is some problem in Calender_Object part.
Looking for a solution.
Thanks in advance.
Please note: Alarms will be executed immediately, if the notification time has elapsed already.
As a workaround you might want to consider a date part too. Or just a variable in memory which acts as a boolean if the time has elapsed or not.
From the Docs:
If the stated trigger time is in the past, the alarm will be triggered immediately. If there is already an alarm for this Intent scheduled (with the equality of two intents being defined by filterEquals(Intent)), then it will be removed and replaced by this one.
Also please consider the API level 19 version of how AlarmManager works.

Android Alarm Clock - Remove alarms programmatically

I am writing an application in which user can configure alerts/alarms. At this point, I have everything working expect the one. I am firing off an alarm using
Intent alarmIntent = new Intent(AlarmClock.ACTION_SET_ALARM);
alarmIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
alarmIntent.putExtra(AlarmClock.EXTRA_MESSAGE, "Some message!");
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MINUTE, 1);
alarmIntent.putExtra(AlarmClock.EXTRA_HOUR, calendar.get(Calendar.HOUR_OF_DAY));
alarmIntent.putExtra(AlarmClock.EXTRA_MINUTES, calendar.get(Calendar.MINUTE));
alarmIntent.putExtra(AlarmClock.EXTRA_SKIP_UI, true);
context.startActivity(alarmIntent);
I want to remove this alarm once user has dismissed using the Dismiss button. I can see the alarms being still there in the alarm clock which I set using above code through my application.
Is there some way to get a cursor or something similar on the alarms being there in the alarm clock? This will help me iterate over them and remove the ones I want.
Any help would be appreciated, Thanks in advance.
As it was answered here: there is no supported API to this.
Official documentation says that
If a time of day is specified, and EXTRA_SKIP_UI is true, and the
alarm is not repeating, the implementation should remove this alarm
after it has been dismissed.
But different manufactures implement their own alarm clocks and I'm not sure if someone supporrts this.
I have tried it on JB emulator and looks like it dosen't work. Maybe, on KitKat?
Only this one is ok on JB:
If an identical alarm exists matching all parameters, the implementation
may re-use it instead of creating a new one
So, maybe, better solution is to use youre own alarm, created with AlarmManager or warn user that he has to remove Alarms by hands (to make it more obvious - not use AlarmClock.EXTRA_SKIP_UI).
call method cancel(...) from AlarmManager, using the same PendingIntent you used to set the alarm. Example:
mAlarmPendingIntent = PendingIntent.getActivity(this, requestCode, intent, flags);
this.getAlarmManager().cancel(mAlarmPendingIntent);
this refers to the Activity or the Service from which you are cancelling the alarm

AlarmManager triggers PendingIntent too soon

I've searched for 3 days now but didn't find a solution or similar problem/question anywhere else. Here is the deal:
Trigger in 1 hour -> works correct
Trigger in 2 hours -> Goes of in 1:23
Trigger in 1 day -> Goes of in ~11:00
So why is the AlarmManager so unpredictable and always too soon? Or what am I doing wrong? And is there another way so that it could work correctly?
This is the way I register my PendingIntent in the AlarmManager (stripped down):
AlarmManager alarmManager = (AlarmManager)parent.getSystemService(ALARM_SERVICE);
Intent myIntent = new Intent(parent, UpdateKlasRoostersService.class);
PendingIntent pendingIntent = PendingIntent.getService(parent, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//Set startdate of PendingIntent so it triggers in 10 minutes
Calendar start = Calendar.getInstance();
start.setTimeInMillis(SystemClock.elapsedRealtime());
start.add(Calendar.MINUTE, 10);
//Set interval of PendingIntent so it triggers every day
Integer interval = 1*24*60*60*1000;
//Cancel any similar instances of this PendingIntent if already scheduled
alarmManager.cancel(pendingIntent);
//Schedule PendingIntent
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, start.getTimeInMillis(), interval, pendingIntent);
//Old way I used to schedule a PendingIntent, didn't seem to work either
//alarmManager.set(AlarmManager.RTC_WAKEUP, start.getTimeInMillis(), pendingIntent);
It would be awesome if anyone has a solution. Thanks for any help!
Update:
2 hours ago it worked to trigger it with an interval of 2 hours, but after that it triggered after 1:20 hours. It's getting really weird. I'll track the triggers down with a logfile and post it here tomorrow.
Update:
The PendingIntent is scheduled to run every 3 hours. From the log's second line it seems like an old scheduled PendingIntent is still running:
[2012-5-3 2:15:42 519] Updating Klasroosters
[2012-5-3 4:15:15 562] Updating Klasroosters
[2012-5-3 5:15:42 749] Updating Klasroosters
[2012-5-3 8:15:42 754] Updating Klasroosters
[2012-5-3 11:15:42 522] Updating Klasroosters
But, I'm sure I cancelled the scheduled PendingIntent's before I schedule a new one. And every PendingIntent isn't recreated in the same way, so it should be exactly the same. If not , this threads question isn't relevant anymore.
When using a calendar are you taking into account that the calendar uses the time right down to Milli seconds. Maybe you should set the Milli second field and the seconds field to zero so it's going of on the dot.
Also for a day it would be easier to use this
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(0);
cal.add(Calendar.DAY_OF_MONTH, 1);
Also when you use getInstance doesn't that set the calendars time to the time it was created so there shouldn't be any need to set the time again right?
Rewrite: I eventually saw your error, but unpredictably.
I did changed this:
PendingIntent.getService(parent, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
to this:
PendingIntent.getService(parent, 0, myIntent, PendingIntent.FLAG_CANCEL_CURRENT);
under the same assumption as you that somehow an old intent is broadcasting. I haven't seen the fluke since...
Also the only times I saw it were during my initial call. Another approach could be to track a current and a previous Calendar object, if the interval isn't what you expected then ignore this "early" broadcast. (While this method seems redundant considering how the alarm should work, it helps prevent those extraneous calls considering how the alarm is working...)
Hope that helps, I'll let you know if I find anything else.
I know this question is a bit old, but I had this same problem myself. I found out that if I tried to declare the Calendar variable outside of the method, it wouldn't play nicely and the alarms would fire early. Because your class is stripped down it is hard to tell exactly where you're calling the calendar instance.
If I set it up as such, then it would fire right on time:
protected void nextAlarm(Context context, int seconds){
Calendar nextAlarm = Calendar.getInstance();
Intent intent = new Intent(context, MyClass.class);
PendingIntent pending = PendingIntent.getBroadcast(context, MainActivity.REPEATING_ALARM, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager amanager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
nextAlarm.add(Calendar.SECOND, seconds);
amanager.set(AlarmManager.RTC_WAKEUP, nextAlarm.getTimeInMillis(), pending);
}
Make sure your service's onStartCommand returns START_NOT_STICKY, otherwise it will be automatically re-attempted:
public class UpdateKlasRoostersService extends Service {
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
buildUpdate();
return START_NOT_STICKY;
}
}

Categories

Resources