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
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.
I am new to android , I want to make alarm that notify me dialy at 11am .I have found some code on net,But dont know some part of it. Below is code
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(AndroidAlarmService.this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(AndroidAlarmService.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis()); does
calendar.add(Calendar.SECOND, 10);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5*1000, pendingIntent);
Toast.makeText(AndroidAlarmService.this, "Start Alarm", Toast.LENGTH_LONG).show();
}});
can any one explain me what this two line does
calendar.setTimeInMillis(System.currentTimeMillis()); does
calendar.add(Calendar.SECOND, 10);
And also help me how can i set alarm that will notify me on the same time for example at 11am
Call this function where you need:
private void setDailyNotification(int ID, int hh, int mm, int ss) {
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent alarmIntent = new Intent(Dashboard.this, MyDailyReceiver.class);
alarmIntent.putExtra("ID", ID);
Log.d("setDailyNotification", "ID:" + ID);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
Dashboard.this, ID, alarmIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mEverydayPendingIntent = pendingIntent;
Calendar calendar = Calendar.getInstance();
Calendar now = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hh);
calendar.set(Calendar.MINUTE, mm);
calendar.set(Calendar.SECOND, ss);
// check whether the time is earlier than current time. If so, set it to
// tomorrow. Otherwise, all alarms for earlier time will fire
if (calendar.before(now)) {
calendar.add(Calendar.DATE, 1);
}
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
mEverydayPendingIntent);
Log.d("setRepeated", "ID:" + ID);
}
Broadcast receiver:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
context);
mBuilder.setContentTitle("Daily Summery");
mBuilder.setContentText("Today's Transaction");
mBuilder.setSmallIcon(R.drawable.app_icon);
Log.d("tag1234", "In if" + daily_Reminder);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
mBuilder.setStyle(inboxStyle);
mNotificationMa = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationMa.notify(11, mBuilder.build());
call function like this:
setDailyNotification(11, 11,00, 00);
System.currentTimeMillis()- gets the current time from the device.
calendar.setTimeInMillis()- sets the current time.
calendar.add(Calendar.SECOND, 10) - adds ten seconds to the current time.
In total, you are fetching the current time of the device and adding ten seconds in it.
You can try this Worked for me
int interval = 1000 * 60 * 1;
// 1 for minutes ,you can pass 60*24
//And set alarm manager like this
Intent alarmIntent = new Intent(AlaramManagerActivity.this, NotificationReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(AlaramManagerActivity.this, RQS_1, alarmIntent, 0);
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),interval, pendingIntent);
Intent myIntent = new Intent(activity.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,11); // At the hour you wanna fire
firingCal.set(Calendar.MINUTE, 0); // Particular minute
firingCal.set(Calendar.SECOND, 0); // particular second
long intendedTime = firingCal.getTimeInMillis();
long currentTime = currentCal.getTimeInMillis();
if(intendedTime >= currentTime){
// you can add buffer time too here to ignore some small differences in milliseconds
// set from today
alarmManager.setRepeating(AlarmManager.RTC, intendedTime, AlarmManager.INTERVAL_DAY, pendingIntent);
}
else{
// set from next day
// you might consider using calendar.add() for adding one day to the current day
firingCal.add(Calendar.DAY_OF_MONTH, 1);
intendedTime = firingCal.getTimeInMillis();
alarmManager.setRepeating(AlarmManager.RTC, intendedTime, AlarmManager.INTERVAL_DAY, pendingIntent);}
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);
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.
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?