I´m making a widget app and I want a part of it update once a day with android:updatePeriodMillis="86400000" and another part update every minute with AlarmManager. But I think if you use AlarmManager you have to put to android:updatePeriodMillis="0". Is it possible to do that?
I believe that you can use both updatePeriodMillis and AlarmManager simultaneously. But in any case, you can always rely on AlarmManager for both updates, setting two different "Alarms" for the same widget update.
Finally this work (two alarms, one every minute and another every day):
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.SECOND, 00);
alarmManager.setRepeating(AlarmManager.RTC,
calendar.getTimeInMillis(),
60000, funtion1(context));
Calendar calendar2 = Calendar.getInstance();
calendar2.set(Calendar.HOUR_OF_DAY, 00);
calendar2.set(Calendar.MINUTE, 00);
calendar2.set(Calendar.SECOND, 00);
alarmManager.setRepeating(AlarmManager.RTC,
calendar2.getTimeInMillis(),
86400000, funtion2(context));
Related
I'm currently making a native Android app and I have code working to schedule a notification to appear on the device using the AlarmManager class:
Intent intent = new Intent(this, NotifyActivity.class);
AlarmManager alarmMgr = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
PendingIntent alarmIntent = PendingIntent.getService(this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000, alarmIntent);
Two things:
The notification goes off every time the app is opened. I don't want this to happen.
The notifications is suppose to be going off at 9am device time (or so I'm led to believe). This is not happening and it looks like it goes off every 9 hours or so.
Can anyone tell me why the notification goes off every time the app is opened and why the notification is not only being triggered when the device reaches 9am?
Thanks
From the doc of AlarmManager:
If the stated trigger time is in the past, the alarm will be
triggered immediately
You are setting your alarm to 9:00 , if you launch after this hour, trigger time is in the past, so the alarm is going off when the app is opened.
You can check if the current time is after alarm, if so, you add one day to the alarm and it will going off the next day. For example:
Calendar cal = Calendar.getInstance();
Calendar cal_now = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.HOUR_OF_DAY, 9);
cal.set(Calendar.MINUTE, 0);
if (cal.before(cal_now)) {//if its in the past
cal.add(Calendar.DATE, 1);
}
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 24*60*60*1000, alarmIntent);
Hope it helps,
This question already has answers here:
AlarmManager fires alarms at wrong time
(2 answers)
Closed 7 years ago.
I have four Repeating Alarms in my app each of which fires at different timings to do some important tasks.
They are
Alarm 1 - 12:30 AM
Alarm 2 - 01:00 AM
Alarm 3 - 06:00 AM
Alarm 4 - 12:15 PM
Every alarm fires no problems. Time interval for each alarms are 24 hrs. But each of the firing at random timings.
Alarm Set Time Actual Firing Time
Alarm 1 12:30 AM 06:31 AM
Alarm 2 01:00 AM 06:01 AM
Alarm 3 06:00 AM 06:01 AM
Alarm 4 12:15 PM Still not fired 4 hours after that time
My Code was
Alarm 1
Intent myIntent=new Intent(MainPage.this,db_restore.class);
PendingIntent pi=PendingIntent.getBroadcast(MainPage.this,1,myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, 1);
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND,0);
calendar.set(Calendar.AM_PM,Calendar.AM);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),1000*60*60*24,pi);
Alarm 2
Intent mor_flag=new Intent(MainPage.this,MorningFlag.class);
PendingIntent mor_intent=PendingIntent.getBroadcast(MainPage.this,3,mor_flag,0);
AlarmManager mor_alarm=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Calendar calendar=Calendar.getInstance();
calendar.add(Calendar.DATE, 1);
calendar.set(Calendar.HOUR_OF_DAY, 1);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND,0);
calendar.set(Calendar.AM_PM,Calendar.AM);
mor_alarm.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),1000*60*60*24,mor_intent);
Alarm 3
Intent late_start1=new Intent(MainPage.this,late_start.class);
PendingIntent piLate=PendingIntent.getBroadcast(MainPage.this,2,late_start1,0);
AlarmManager alarmManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Calendar calendar=Calendar.getInstance();
calendar.add(Calendar.DATE, 1);
calendar.set(Calendar.HOUR_OF_DAY, 6);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND,0);
calendar.set(Calendar.AM_PM,Calendar.AM);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000*60*60*24, piLate);
Toast.makeText(MainPage.this,"Alarm Service started for Late Start monitoring",Toast.LENGTH_LONG).show();
Alarm 4
Intent eve_flag=new Intent(MainPage.this,EveningFlag.class);
PendingIntent eve_intent=PendingIntent.getBroadcast(MainPage.this,4,eve_flag,0);
AlarmManager eve_alarm=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Calendar calendar=Calendar.getInstance();
calendar.add(Calendar.DATE, 1);
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 15);
calendar.set(Calendar.SECOND,0);
calendar.set(Calendar.AM_PM, Calendar.PM);
eve_alarm.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),1000*60*60*24,eve_intent);
I don't know what I'm doing wrong. Please take a look and help me. Thanks.
Note: I'm checking this in API Level 19 Android 4.4.4 with minimum as 14.
From the API
DOCS(https://developer.android.com/training/scheduling/alarms.html)
As of Android 4.4 (API Level 19), all repeating alarms are inexact. This means Android synchronizes repeating alarms from multiple apps and fires them at the same time.
So if you want an exact alarm use Alarm Manager's
setExact (int type, long triggerAtMillis, PendingIntent operation)
method.
If you want repeating alarms with exact timing then you would have to write a logic where you will set an alarm (Not a repeating alarm) and when this alarm goes off set another alarm for next interval.
Try using AlarmManager.setExact() and give some rep to this post
For api levels below 19 you should use AlarmManager.setRepeating() and your alarms will trigger exactly at specified time. Thou on api levels 19 and above this will no longer work. There was change in android so that all repeating alarms are inexact. So if you would like to achieve exact repeating alarm you should schedule alarm with AlarmManager.setExact() and then when alarm triggers do it again for next week and so on every week.
I realize that this alarm triggers even after finish time in android is a similar question. However, mine's got a little bit different.
I wonder, for example, if i set time to be 14:40, and launch the app at 15:00 (or any other time after 14:40:00), the alarm manager's onReceive method will be triggered immediately.
Actually, this is a confirmation rather than a question since my application seems to do this.
Thank you !
CODE
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 14);
c.set(Calendar.MINUTE, 40);
c.set(Calendar.SECOND, 0);
//System.out.println();
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
Try with the AlarmManager.setExact()
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
I am starting my alarm at 8am using below code, repeating at every 1min. I want to stop this alarm repeating at 12pm. I am starting alarm like this:
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
PendingIntent loggerIntent1 = PendingIntent.getBroadcast(this, 0,new Intent(this,AlarmReceiver.class), 0);
Calendar timeOff9 = Calendar.getInstance();
timeOff9.set(Calendar.HOUR_OF_DAY, 08);
timeOff9.set(Calendar.MINUTE, 00);
timeOff9.set(Calendar.SECOND, 00);
long duration = interval * 60 * 1000;
manager.setRepeating(AlarmManager.RTC_WAKEUP,timeOff9.getTimeInMillis(), duration, loggerIntent1);
and stopping with this code:
Calendar timeOff = Calendar.getInstance();
timeOff.set(Calendar.HOUR_OF_DAY,12);
timeOff.set(Calendar.MINUTE,00);
timeOff.set(Calendar.SECOND, 00);
manager.set(AlarmManager.RTC_WAKEUP,timeOff.getTimeInMillis(), loggerIntent1);
manager.cancel(loggerIntent1);
loggerIntent1.cancel();
but the code for stopping causes the app to work the wrong way.
I am writing the code to stop alarm right after first alarm. is it the problem?
Do I need to write another alarm. Please guide.
Maybe there is another logic you could give a try?
Imagine this, carry on with your first portion of code where you trigger an intent for every duration.
But inside your AlarmReceiver.class, that will be the one checking current time, and do the AlarmManager.cancel() when the time is over 12pm.
I wanted to know How I can set Alarm for a particular time. For example I want to set alarm for
morning 9am daily. I googled a lot but found only the way to set alarm for a given interval only. like after 2 hours or next day.
hope this code helps you
Calendar calendar = Calendar.getInstance();
//9 AM
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pi = PendingIntent.getService(context, 0, new Intent(context, YourClass.class), PendingIntent.FLAG_UPDATE_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
you should create BroadcastReceiver to receive intent.
read the documentation for further details
I googled a lot but found only the way to set alarm for a given interval only. like after 2 hours or next day.
The second parameter to setRepeating() on AlarmManager is when you want the alarm to go off first. Set that to be 9am tomorrow using a Calendar object, and use an RTC or RTC_WAKEUP alarm.