I want to set alarm between two dates.
For example date1 = 02/12/2105 and date2 = 21/12/2015
alarm should fire every day between date1 and date2
I have tried using below code
private void setAlarm(Calendar targetCal){
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, targetCal.getTime(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
Related
I am using Alarm Manager to schedule different jobs to be executed at different hours of the day. When i am scheduling only one job it is working expectedly, but when i schedule multiple jobs, only the last scheduled gets executed. Below are my codes.
private void triggerAlarm(int hr, int min, String sid, String cid) {
Intent intentToFire = new Intent(getApplicationContext(), AlarmBroadcastReceiver.class);
intentToFire.putExtra("cid", cid);
intentToFire.putExtra("sid", sid);
intentToFire.setAction(ACTION_ALARM);
PendingIntent alarmIntent = PendingIntent.getBroadcast(getApplicationContext(),
0, intentToFire, 0);
AlarmManager alarmManager = (AlarmManager) getApplicationContext().
getSystemService(Context.ALARM_SERVICE);
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, hr);
c.set(Calendar.MINUTE, min);
c.set(Calendar.SECOND, 0);
long afterTwoMinutes = c.getTimeInMillis();
alarmManager.set(AlarmManager.RTC_WAKEUP, afterTwoMinutes, alarmIntent);
//sendBroadcast(intentToFire);
}
You can use id for your AlarmReceiver I Also Using this and hope it's also works for you
in targetCal Param just pass your calander obje where you selecting time for alarm
like this
private void setAlarm(Calendar targetCal) {
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
final int _id = (int) System.currentTimeMillis();
PendingIntent appIntent = PendingIntent.getBroadcast(this, _id, intent,PendingIntent.FLAG_ONE_SHOT);
// PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(),
appIntent);
}
Hope it works for you.
How do i use the two vales i have (hour and minute ) to set and alarm at that time? The values i have unfortuntly arent formated so if hour is > 12 it wont go to one (Its in milatary time format) .Did i set up the pending intent correctly?
public void setUpAlarm(int hour,int minute) {
long newTime;
Intent alarm = Intent(setAlarmList.this, AlarmReciever.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
PendingIntent waiting = PendingIntent.getBroadcast(setAlarmList.this, 0, alarm, 0);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, hour.);
use below code
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, h); // h->hour in 24 hrs system
cal.set(Calendar.MINUTE, m); // m->minutes
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
if (android.os.Build.VERSION.SDK_INT>16)
{
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);
}else
{
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), alarmIntent);
} //alarmIntent is your PendingIntent
read this documentation
I am setting the alarm in one activity, and want to cancel it in another class which is a service. Following is my code:
Intent intent = new Intent(SetAlarm.this, AlertFriend.class);
PendingIntent pi = PendingIntent.getActivity(SetAlarm.this, unique_id, inent,
PendingIntent.FLAG_UPDATE_CURRENT);
Calendar c1 = Calendar.getInstance();
c1.set(Calendar.HOUR_OF_DAY, hour);
c1.set(Calendar.MINUTE, min);
c1.set(Calendar.SECOND, 0);
c1.set(Calendar.MILLISECOND, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, c1.getTimeInMillis(), pi);
And following is where i am deleting the alarm (where a is pi):
Intent intent = new Intent(context, AlertFriend.class);
AlarmManager alarm = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(PendingIntent.getActivity(context, a, intent,PendingIntent.FLAG_UPDATE_CURRENT));
I'd like to show a dialog at specific date and time (25/12/2012 at 12.00) and I am using this code. I set like 11 month (because 0 is gen) but the alarm does not start. What is my mistake?
Calendar cal=Calendar.getInstance();
cal.set(Calendar.MONTH,11);
cal.set(Calendar.YEAR,2012);
cal.set(Calendar.DAY_OF_MONTH,25);
cal.set(Calendar.HOUR_OF_DAY,12);
cal.set(Calendar.MINUTE,00);
cal.set(Calendar.SECOND,0);
Intent _myIntent = new Intent(context, Notify.class);
PendingIntent _myPendingIntent = PendingIntent.getBroadcast(context, 123, _myIntent, PendingIntent.FLAG_UPDATE_CURRENT| Intent.FILL_IN_DATA);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), _myPendingIntent);
Look at below code about how i am going to set the alarm for the December month:
// for Alarm 25/12/2012 at 12.00
Calendar myAlarmDate = Calendar.getInstance();
myAlarmDate.setTimeInMillis(System.currentTimeMillis());
myAlarmDate.set(2012, 11, 25, 12, 00, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent _myIntent = new Intent(context, AlarmReceiverNotificationForEveryMonth.class);
_myIntent.putExtra("MyMessage","HERE I AM PASSING THEPERTICULAR MESSAGE WHICH SHOULD BE SHOW ON RECEIVER OF ALARM");
PendingIntent _myPendingIntent = PendingIntent.getBroadcast(context, 123, _myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, myAlarmDate.getTimeInMillis(),_myPendingIntent);
You can update the above code with your intent and class and you will get your desire output.
Hope this help you.
I want to set the alram for phone through program coding. I have an button where by clicking DatePicker come & also time picker. I want what time i set here that time should be set in phones alaram. How can I do that . plz give me the answer . Thank you
Please try this....
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_YEAR, 0);
cal.set(Calendar.HOUR_OF_DAY, 9);
cal.set(Calendar.MINUTE, 01);
cal.set(Calendar.SECOND, 0);
Intent intent = new Intent(Alarm.this, Alarm1.class);
PendingIntent sender = PendingIntent.getBroadcast(this, 1234567, intent, 0);
PendingIntent sende2 = PendingIntent.getBroadcast(this, 123123, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sende2);
Intent intent1 = new Intent(Alarm.this, Alarm1.class);
PendingIntent sender1 = PendingIntent.getBroadcast(this, 1234567, intent1, 0);
AlarmManager am1 = (AlarmManager) getSystemService(ALARM_SERVICE);
am1.cancel(sender1);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Alarm a = new Alarm ();
a.setAlarm();
b1.setText(prod);
}
});