I set an AlarmManager to open notification every 24 hours.
But every time I open the activity, the notification starts.
So how to set it not to show the notification when the activity is already opened?
This is my AlarmManager code:
AlarmManager alarmManager = (AlarmManager) this.getSystemService(this.ALARM_SERVICE);
Intent intent = new Intent(this, myService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
long when = calendar.getTimeInMillis(); // notification time
calendar.set(Calendar.HOUR_OF_DAY, 20);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);
alarmManager.setRepeating(AlarmManager.RTC, when, 24*60*60*1000, pendingIntent); //every 24 hours
Thanks ,
Your when variable points to the current time, hence the alarm will be fired right at execution of that part. Change when to calendar.getTimeInMillis(). Your calendar variable is set to the hour 20 of the day and repeating the day after. Here is a working copy
Related
I am using the following code to set a repeating alarm in android to trigger at every 12am but it not getting trigger even at one.
I need this alarm to trigger from next day 12 am hence I added one date in calendar object.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 00);
calendar.set(Calendar.MINUTE,00);
calendar.set(Calendar.SECOND,00);
calendar.add(Calendar.DATE,1);
Utils.printLog("date from repeating alarm "+calendar.getTime());
Intent startIntent = new Intent(context, RepeatingAlarmReceiver.class);
PendingIntent startPIntent = PendingIntent.getBroadcast(context, 15, startIntent, 0);
AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (alarmMgr != null) {
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, startPIntent);
}
I tested by breakpoint and found that alarmMgr is not null on setting it.
Also the RepeatingAlarmReceiver.class is working.
I am working on app in which I want to set alarm on weekly basis, e.g. on every sunday 10:00pm or every sunday and monday on 10:00pm. but whenever I start alarm manager it triggers immediately, I know that I am creating an alarm for the day which has passed but I still want to add alarm for next coming sunday, and I am unable to achieve that. here is a code what I have done so far.
Intent intent = new Intent(getBaseContext(), SchedulerReciever.class);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(getBaseContext(), i, intent, 0);
intentArray.add(pendingIntent1);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, 1);
calendar.set(Calendar.HOUR, 10);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM, Calendar.PM);
Long alarmTime = calendar.getTimeInMillis();
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, AlarmManager.INTERVAL_DAY*7,alarmTime, pendingIntent1);
I have set one alarm in repeated mode for every 24 hours. So Alarm receiver should be call on the time set only once but it calls repeatedly. Why?
Here is I am attached my code to set alarm:
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent objIntent = new Intent(this, PedometerAlarmReciever.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, objIntent, 0);
// Set the alarm to start at 21:32 PM
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR, 11);
calendar.set(Calendar.MINUTE, 20);
calendar.set(Calendar.AM_PM, Calendar.AM);
// setRepeating() lets you specify a precise custom interval--in this case,
// 1 day
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
I am trying to trigger action everyday at 00:00:00 AM using AlarmManager but the problem is first time action is triggered quickly and then works as expected. First time, action triggers as soon as the code is run. Please see the following code:
private void setAlarmManagerForDateChange()
{
Intent intent = new Intent(this, DateTimeChangeReceiver.class);
intent.putParcelableArrayListExtra("names", names);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,
999, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),
24 * 60 * 60 * 1000, pendingIntent);
}
It action triggers before 00:00:00 AM for the first time. Please point out what is being missed. Thanks,
You're scheduling the alarm in the past, which causes the AlarmManager to fire instantly.
You take the current date (e.g. 05/29/14 20:08:32), and set the hour, minute and second to 0.
What you get is: 05/29/14 00:00:00.
What you actually want, is to add another day to get to 06/29/14 00:00:00.
calendar.add(Calendar.DAY, 1);
The below code triggers alarm randomly at any time, but I want to trigger it only at 8 AM and daily.
What I am missing, please help. Thanks in advance.
Intent alarmIntent = new Intent(context, NotifyingDailyService.class);
PendingIntent alarmPIntent = PendingIntent.getService(context, 0, alarmIntent, 0);
// Set the alarm to start at approximately 8:00 a.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 0);
AlarmManager alarmMgr= (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmMgr.cancel(alarmPIntent);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmPIntent);
I believe AlarmManager.INTERVAL_DAY is only appropriate for calls to setInexactRepeating(). I think you need to change AlarmManager.INTERVAL_DAY to the number of milliseconds in a day.
INTERVAL_DAY DOCUMENTATION
http://developer.android.com/reference/android/app/AlarmManager.html#INTERVAL_DAY
ALARM MANAGER EXAMPLE
Android: How to repeat a service with AlarmManager every 15 minutes, but only run from 8:00AM to 18:00PM?
I solved the question myself by using broadcast rather than service.
Intent alarmIntent = new Intent(context, DailyNotificationAlarmReceiver.class);
PendingIntent alarmPIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
// Set the alarm to start at approximately 8:00 a.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 0);
//System.out.println("Alarm set at 8 clock");
Toast.makeText(context, "Alarm set at 8 clock", Toast.LENGTH_LONG).show();
AlarmManager alarmMgr= (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
// With setInexactRepeating(), you have to use one of the AlarmManager interval
// constants--in this case, AlarmManager.INTERVAL_DAY.
alarmMgr.cancel(alarmPIntent);
//alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmPIntent);
//alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),calendar.getTimeInMillis()+ DateUtils.DAY_IN_MILLIS, alarmPIntent);
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, alarmPIntent);