i want to set alarm to repeat it in a particular day .for example every monday at 4:20 ,
I have created a method but not working .please help me to solve this problem
public void setAlarm(int day,int hour,int minit){
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE,minit );
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
cal.set(Calendar.DAY_OF_WEEK, day);
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Long alarmTime = cal.getTimeInMillis();
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, 24 * 60 * 60 * 1000 *7, pendingIntent);
}
now if i choose monday 2:40 its only alarming on next monday 2:40 not working on every monday 2:40
You can try use setInexactRepeating instead.As of API 19, all repeating alarms are inexact.You can refer to the official documents in detail.
Use setInexactRepeating function it worked for me.
Related
Below is my code, it works great for the first 7 or so days but after that it just stops. I am not sure why?
I am having much the same problem as this person:
here
Any help will be appreciated:)
Intent _intent = new Intent(context, AlarmBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, _intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, min);
calendar.set(Calendar.SECOND, sec);
// every 24 hours (daily) at the time set
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * minTime * MainSetTimeInt, pendingIntent);
I'm programming an app that has a feature that the user can use to set an alarm for an SMS message to be send to someone. The problem I am having is say the user sets the Alarm-Manager to go off at 9:00 A.M. and it is currently 10:00 P.M. then the alarm immediately goes off. It doesn't recognize the fact that the alarm should be set for the next day.
Here is the code for setting up the AlarmManager:
Intent intentAlarm = new Intent(MainMenu.this, AlarmReceiver.class);
intentAlarm.putExtra("phoneNumber", phoneNumber);
intentAlarm.putExtra("message", message);
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), PendingIntent.getBroadcast(MainMenu.this, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
Other than the AM/PM issue, the AlarmManager works well. I just don't know where to go from here and all of the documentation I am looking at just doesn't help.
Calendar cal1 = Calendar.getInstance();
cal1.set(Calendar.HOUR_OF_DAY, 10); // use hour of day for am pm issue
cal1.set(Calendar.MINUTE, 00);
cal1.set(Calendar.SECOND, 00);
Calendar now = Calendar.getInstance();
if (now.after(cal1))
cal1.add(Calendar.DAY_OF_YEAR, 1);
// this code snippet add one day if the current time passed away
//else use cal1.set(year, month, day); for exact day
public void SetAlarm(Context context) {
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
// set alarm after 12 hours
long after = System.currentTimeMillis() + 12 * 60 * 60 * 1000;
am.set(AlarmManager.RTC_WAKEUP, after, pi);
}
I am developing an android app for setting the alarm on monthly basis. I have given the below code for next month.
private int GetTotalDays(int monthRecvd1) {
int totalDays=getDaysInMonthInPresentYear(taskMonth1);
myDays=(totalDays-taskdate1)+taskdate1;
}
private int getDaysInMonthInPresentYear(int taskMonth1)
{
int days=0;
alarmCalendar.set(Calendar.HOUR, Hrs);
alarmCalendar.set(Calendar.MINUTE, Mins);
alarmCalendar.set(Calendar.SECOND, 0);
alarmCalendar.set(Calendar.DATE, date);
alarmCalendar.set(Calendar.MONTH, Month);
alarmCalendar.set(Calendar.YEAR, year);
Long alarmTime = alarmCalendar.getTimeInMillis();
calendar.set(year, Month, date);
days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
}
This code works only for next month. and doesn't work on second next month. for example, if i set the alarm on april, it will set for may but not for june and so on.
Can anybody tell me what could be the solution for this.
Please help! Thanks!
Use setRepeating(int type, long triggerAtMillis, long intervalMillis, PendingIntent operation) method rather than just AlarmManager.set(). You can find more info on documentation.
http://developer.android.com/reference/android/app/AlarmManager.html
Edit: It should like this
public void onClick(View arg0) {
Intent serviceIntent = new Intent(AndroidAlarmService.this, this);
pendingIntent = PendingIntent.getService(AndroidAlarmService.this, 0, serviceIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
long now = calendar.getTimeInMillis();
calendar.add(Calendar.DATE,calendar.getActualMaximum(Calendar.MONTH));
long repeat = calendar.getTimeInMillis();
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, now , repeat , pendingIntent);
}
try this..
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_DAY,
AlarmManager.INTERVAL_DAY, intent);
The solution is to explicitly force the literals to be of type long, which is a 64-bit number:
am.setRepeating(AlarmManager.RTC_WAKEUP, alarmTimefor30, 30L*1440L*60000L , pi);
Source:
Setting alarm on monthly basis android
I am trying to create an alarm on the first Monday of each month. Here is my code:
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, FilterOrderBroadcastReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.WEEK_OF_MONTH, 1);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
if(Calendar.getInstance().getTimeInMillis() - calendar.getTimeInMillis() > 0 ) {
calendar.add(Calendar.MONTH, 1);
}
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis() - calendar.getTimeInMillis(), pi);
Today is the 12th of June. This code creates an alarm trigger that is 1382399999 millis away or 15.9 days. That will only take me to the end of the 27th. I have thought about doing it with Joda, but there are issues with the Joda library with Android. It is really slow. Can anyone show me how to do this with the native Android libraries? Thanks for the help.
Hi i've been researching the alarm manager in android and was wondering if there how to set a specific time for an alarm (or could use notification manager)to go off at a specific time for example 12pm tomorrow .
The code below sets the alarm for 5 seconds from now so to set it for something like 12 pm could you do something like 12:00:00 or something?
Intent intent = new Intent(this, OnetimeAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (5 * 1000), sender);
Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();
Java's Date and Time libraries are a huge hassle, but this should give you an idea:
// the time now
Calendar calendar = Calendar.newInstance();
// noon tomorrow
calendar.add(Calendar.DAY_OF_YEAR, 1);
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();
Calendar calendar = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
this should work