Repeat scheduled tasks on selected days in Android sdk alarm manager - android

I have tried to do this without bothering the experts and have read numerous threads here and on other sites. It is clearly my brain not understanding what needs to be done in order for this to work.
My goal is that the app allows the user to enter a time and one or more days in a week. All of the GUI side and storing of the dates and times I have done, however to get the alarm manager to repeat, lets say every Monday at 14:00 and then can send at 14:02 . I have used the java Calendar object to hold the times and days of the week or even used date and day of the week of the month. These are then , as needed, converted to milliseconds for it to be read in by the alarm manager.
I then have used either the alarm manager set or set repeat methods to repeat the event. All I am able to do is get it to occur once and then if I change the emulator date and time to another Monday nothing happens.
The GUI holds the hours and minutes in required variables and then these are used against the calendar objects.
The alarm manager calls a broadcast receiver for the event to occur.
Please can someone simply give an example on how to set specific days such as Monday , Wednesday Friday. I know that separate alarm managers are needed for each day and at the moment I have just focused on Monday as my main test.
Links viewed:
How can i Repeat the Alarm in android for only Monday, Tuesday and Friday
How to repeat the alarm for "n" days at a particular time
how to repeat alarm after 1 day in android
Managed to figure this out now and so follows my answer:
The following code calculates the remaining days between now and the day needed for the scheduled task. the variable whichday is passed via parameter from the method this code belongs to. In the understanding of this whichday represents days of the week 1 through to 7 where 1 is Sunday , 2 is Monday and so .
//This gets the current day of the week as of TODAY / NOW
int checkcurrentday = getcurtime.get(Calendar.DAY_OF_WEEK);
// This calculates the days between now and the day needed which is represented by whichday.
int numberofdays = Calendar.SATURDAY + whichday - checkcurrentday;
//Now add NOT set the difference of the days to your Calendar object
tMondayOn.add(Calendar.DATE, numberofdays);

Well, you need to first use the Java Calendar API (or Joda!) to figure out when the next monday is. Set the alarm to to that time in milliseconds then use setRepeating and pass in a long that represents the interval of one week.

Related

How to add time to another time - Android

I have timer for a task. And all the sessions will be added up to each other.
Let's say today user spent 5 minutes
Other day he spent 1 hour, here this one hour will be added to the 5 minutes
and so on..
So it will be the total time in one value..
How can I do this ? Is it by Milliseconds or Date object ?
Date is more useful when you are dealing with actual calendar dates.
If you just want to keep track of time intervals/durations, just have a long variable and keep on adding the durations to this.
EDIT : Long.MAX_VALUE is 9,223,372,036,854,775,807. So, you don't really need to worry about the overflow either.
You can keep track of all the milliseconds using Date().getTime(), which returns the time since Epoch. So whenever you need to add/remove, just take your Date object, invoke .getTime(), and add/remove from the total. Then when you're done, you can convert the milliseconds to whatever format you need.

set repeated reminder that repeats every month

I am taking two dates from the user in my android code. I want to set reminder for every month which are in between those two dates.
If first date is : 1st jan 2015 and other date is 1st Feb 2016 then I want to set reminder for the 1st day every month in between these two dates. I tried using alarm manager but couldnt able to repeat alarm. Please help. thank you
One method might be to have your alarm receiver set the next alarm when it is called (e.g. it activates on 1 Jan 2015 and sets the next one for 1 Feb 2015), so you're always setting one ahead.
First you'll need to create 2 Calendar objects to store the dates to store the dates set by the user. Then proceed by either creating pending intents for each date in a for loop by incrementing the value of month. Assign all the pending intents to your AlarmManager and fire them with FLAG_ONE_SHOT.
Please provide your code so we could better understand your problem.

How to set repeating alarm on week days[Day and Night], Week end in simple manner?

I need to set on repeating alarm for my below cases,
Week Days[Day(8AM -8PM), Night(8PM -8AM)]
Week End
My Application mode changes during each period of time.
I have used the below code to set an alarm for particular day[Monday].
//Setting Alarm on Monday 8AM
Calendar alarmCalendar= Calendar.getInstance();
alarmCalendar.set(Calendar.DAY_OF_WEEK, 2);
alarmCalendar.set(Calendar.HOUR, 8);
alarmCalendar.set(Calendar.MINUTE, 0);
alarmCalendar.set(Calendar.SECOND, 0);
alarmCalendar.set(Calendar.AM_PM, am);
Above code will set the alarm for Monday. How to Set separate alarm for Week days for 8AM and 8PM. How to set Alarm for Week end also. Whether I have to replicate the same above code to set on alarm for each and every day.
Or else any option to set the alarm in simple manner?
Please help me on this.
You can setup the alarm to the Monday with a repetition of every 7 days, and make it for the rest of the work days by the same way.

Android how to map every next monday and thursday, 6 pm for the next four weeks to actual dates?

My app's user enters a few days from the week, and a few times a day when they need to get notified about something.
So in the SQLite DB I have the following information:
days of the week the alarm should go off
hours and minutes of the day the alarm should go off (might be a few times a day)
number of weeks
Now, how do I get this information and map it to a list of real dates, like June 2nd, 2014 15:30 for example?
Also, for the current week, all the reminders that are already passed, should be moved to the end of the queue.
Then you have to calculate date by yourself.
Assume 7 day a week.
For example : Tuesday in next 3 weeks
--> target day should be 7 x 2 + 2
Use Calendar object, they have method called add(field , value)
Prototype code for above sample:
Calendar c = Calendar.getInstance();
c.add(Calendar.DAY, 7x2 + 2);
Date date = c.getTime();
then your need object is date
Save this in miliseconds to db and use it later. Done :)

How android calender setInexactRepeating works?

I am developing small android application. And I want to do something in my application after some minutes. These minutes are not static one these are dynamic ones.
So i am using android calender setInexactRepeating for this.
My code looks like this
Calendar cal = Calendar.getInstance();
// Start something after 4 minutes
cal.add(Calendar.MINUTE, 4);
get_alaram_service().setInexactRepeating(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), 1000*300, get_pendingintent());
So this will work setInexactRepeating after 4 min it will run my pending intent and after that it will keep repeating this for this much amount of time. (1000*300).
So my problem is that in setInexactRepeating 2nd parameter is for at what time I want to start my timer and 3rd parameter for repeating this thing. Now 2nd parameter tales value in milisec. I tried to pass my own value of minutes in milisec like(1000*300) then its not working properly. I don't how its working properly. When I checked cal.getTimeInMillis() it is very big integer number. what is that actually.
Am i doing something wrong need your help thank you...
Although this isn't explicitly stated in the documentation the second parameter (triggerAtMillis) is time in milliseconds since the Epoch. This is what Calendar.getInstance() returns. Calling this method will return the current time. This is a big number, since it is actually the number of milliseconds after 1/1/1970. You then need to add something to it (e.g. 4 minutes) to define when the AlarmManager will first fire.

Categories

Resources