I managed to get the code running but it doesn't work properly. I have put an AlarmManager to at an exact hour, display a text, but the thing is that it appears just in the moment I press the input Button, no matter what hour I introduce.
The code:
final TimePicker desde = (TimePicker) findViewById(R.id.timePicker1);
int desdeHora = desde.getCurrentHour();
int desdeMinuto = desde.getCurrentMinute();
Calendar desde1 = Calendar.getInstance();
desde1.set(Calendar.DAY_OF_WEEK, 3); //MIRAR ESTE 1 SIGNIFICA DOMINGO, 2 LUNES, 3 MARTES
desde1.set(Calendar.HOUR_OF_DAY,desdeHora);
desde1.set(Calendar.MINUTE,desdeMinuto);
Intent intent = new Intent(this, Alarma.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(alarmManager.RTC, (desde1.getTimeInMillis() - System.currentTimeMillis()) , 604800000, pendingIntent);
Sorry if you don't understand something because I'm programming in spanish, ask if you don't understand something.
Try
alarmManager.setRepeating(alarmManager.RTC, (desde1.getTimeInMillis()) , 604800000, pendingIntent);
instead of
alarmManager.setRepeating(alarmManager.RTC, (desde1.getTimeInMillis() - System.currentTimeMillis()) , 604800000, pendingIntent);
By the way, what is 604800000 for?
Related
I'm trying to write code that will run a task on specific hours every day.
I have an hours array that contains integers and I loop through it to set a repeating alarm from that hour and with an interval of a day.
If I just create the service and run the task every 10 seconds or something it does run the AlarmReciever but this code doesn't work after the addition of the Calendar API, what am I doing wrong?
AlarmManager alarmManager;
Calendar current = new GregorianCalendar();
Calendar calendar = new GregorianCalendar();
int[] hours = new int[] {14, 18, 21, 22};
public int onStartCommand(Intent intent, int flags, int startId) {
current.setTimeInMillis(System.currentTimeMillis());
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent1 = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent1, 0);
for (int hour : hours) {
calendar.add(Calendar.DAY_OF_YEAR, current.get(Calendar.DAY_OF_YEAR));
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.DATE, current.get(Calendar.DATE));
calendar.set(Calendar.MONTH, current.get(Calendar.MONTH));
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
return START_STICKY;
}
EDIT: I tried printing out calendar.getTimeInMillis() for every loop and here is the output
I/System.out: 2391249600000
I/System.out: 2643724800000
I/System.out: 2769962400000
I/System.out: 2833038000000
Lets say I take the two first numbers: 2643724800000 - 2391249600000 = 252475200000. 252475200000 / 1000 = 252475200. 252475200 / 60 = 4207920.
I'm pretty sure 4207920 minutes is more than one hour. Why is like this?
Okay, I found out how to fix my problem.
First of all thanks to #MikeL for suggesting to remove calendar.add(Calendar.DAY_OF_YEAR, current.get(Calendar.DAY_OF_YEAR)); because that did fix some stuff, but even after that it didn't work.
To fix the problem I changed PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent1, 0); to PendingIntent pendingIntent = PendingIntent.getBroadcast(this, hour, intent1, 0);
Basically changing the 0 to hour so that every pending intent is different.
In monodroid I want to set an alarm. I would like to have a function I can pass a seconds to and the function creates the alarm at the current time + seconds.
All examples I found were nested in some other projects. I am looking for a general purpose solution.
I found
AlarmManager am = (AlarmManager)getSystemService(alarm);
Intent i= new Intent("MY_INTENT");
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.MINUTE, 2);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi);
and changed it to
AlarmManager am = (AlarmManager)GetSystemService(AlarmService);
Intent i = new Intent("MY_INTENT");
PendingIntent pi = PendingIntent.GetBroadcast(this, 0, i, 0);
seconds = Convert.ToInt16(txtMinutes.Text) * 60;
am.Set(AlarmType.ElapsedRealtimeWakeup,SystemClock.ElapsedRealtime() + (seconds * 1000), pi);
But after seconds passed, literally nothing happens.
What to change?
I have an AlarmManager that is supposed to fire off on the 26th of December 2013. Here is my code:
FestCountdownTimer countdownNotificationTimer = new FestCountdownTimer(
00, 00, 9, 26, 11, 2013);
long timeToEvent = countdownNotificationTimer.getIntervalMillis();
System.out
.println("TIME TO EVENT!!! ------------------ " + timeToEvent);
Intent eventAlarm = new Intent(this, AlarmReceiver.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, timeToEvent, PendingIntent
.getBroadcast(this, 1, eventAlarm,
PendingIntent.FLAG_UPDATE_CURRENT));
But, my alarm is firing off after 1 or 2 minutes. I don't know what seems to be wrong. (Byt the way, the FestCountdownTimer is a class I made to get the interval between now and the future set date. The long timeToEvent; is coming properly.)
Thanks for the help...
I dont know about your FestCountdownTimer class but damn sure
probablay time not set properly(I mean may be mistaken convert time to
millisecond)
Note: This below code is harcode not tested by in actual app
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.clear();
cal.set(year,month,day,hour,min); //set your date here
AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
// cal.add(Calendar.SECOND, 5);
alarmMgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
Hi I want my application to run at specific time daily. for this I am using below code. But it runs only for one time. Whats the mistake here how can i achieve this task.
AlarmManager alarmMgr0 = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent0 = new Intent(this, ActivityStarter.class);
PendingIntent pendingIntent0 = PendingIntent.getBroadcast(this, 0, intent0, 0);
Calendar timeOff9 = Calendar.getInstance();
timeOff9.set(Calendar.HOUR_OF_DAY, 16);
timeOff9.set(Calendar.MINUTE, 13);
timeOff9.set(Calendar.SECOND, 0);
alarmMgr0.setRepeating(AlarmManager.RTC_WAKEUP, timeOff9.getTimeInMillis(),24*60*60*1000,pendingIntent0);
Any help please.
Try this
alarmMgr0 .setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_DAY,
AlarmManager.INTERVAL_DAY, intent);
refer this
I am using AlarmManager to fire Notification. I want that notification fires at 10:30 am of the morning and repeat at every 24 hours. I don't want notification when opening application.
I am posting my code, main problem is that It fires alarm in the nite time also at 10:30 (if phone timezone is of 12 hours). I check this code with modification yest and I got alarm yest nite at 10:30, 1, 4, 7.
Please help me solve the problem, I am trying to solve it from the long time.
Code :
Intent myIntent = new Intent(Splash.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(Splash.this,
0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar firingCal = Calendar.getInstance();
Calendar currentCal = Calendar.getInstance();
firingCal.set(Calendar.HOUR_OF_DAY, 10);
firingCal.set(Calendar.MINUTE, 30);
firingCal.set(Calendar.SECOND, 0);
long intendedTime = firingCal.getTimeInMillis();
long currentTime = currentCal.getTimeInMillis();
if (intendedTime >= currentTime) {
alarmManager.setRepeating(AlarmManager.RTC, intendedTime,
AlarmManager.INTERVAL_DAY, pendingIntent);
} else {
firingCal.add(Calendar.DAY_OF_MONTH, 1);
intendedTime = firingCal.getTimeInMillis();
alarmManager.setRepeating(AlarmManager.RTC, intendedTime,
AlarmManager.INTERVAL_DAY, pendingIntent);
}
Try using:
firingCal.set(Calendar.HOUR_OF_DAY, 10);
because this is the hour in 24hour notation.
It also seems you forget to add a day in the else statement because the if and the else do exactly the same now.