Related
I have searched a lot of places but couldnt find a clean sequential explanation of how to start a service (or if thats not possible then an activity) at a specific time daily using the AlarmManager??
I want to register several such alarms and triggering them should result in a service to be started. I'll be having a small piece of code in the service which can then execute and i can finish the service for good....
Calendar cal = Calendar.getInstance();
Calendar cur_cal = Calendar.getInstance();
cur_cal.setTimeInMillis(System.currentTimeMillis());
Date date = new Date(cur_cal.get(Calendar.YEAR), cur_cal.get(Calendar.MONTH), cur_cal.get(Calendar.DATE), 16, 45);
cal.setTime(date);
Intent intent = new Intent(ProfileList.this, ActivateOnTime.class);
intent.putExtra("profile_id", 2);
PendingIntent pintent = PendingIntent.getService(ProfileList.this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pintent);
System.out.println("The alarm set!!");
i tried this code to activate the alarm at 4.45... but its not firing the service... do i have to keep the process running??
M i doing anything wrong???
One more thing, my service gets perfectly executed in case i use the following code:
long firstTime = SystemClock.elapsedRealtime();
alarm.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 30*1000,pintent);
HI friends,
After a lot of researching and with reference from "Pentium10"'s question on the same topic i managed to get it working. Though i still cant understand why the "date" concept and the Calendar(non GregorianCalendar) object which i have mentioned in the question are not working correctly.
Calendar cur_cal = new GregorianCalendar();
cur_cal.setTimeInMillis(System.currentTimeMillis());//set the current time and date for this calendar
Calendar cal = new GregorianCalendar();
cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
cal.set(Calendar.HOUR_OF_DAY, 18);
cal.set(Calendar.MINUTE, 32);
cal.set(Calendar.SECOND, cur_cal.get(Calendar.SECOND));
cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));
Intent intent = new Intent(ProfileList.this, IntentBroadcastedReceiver.class);
PendingIntent pintent = PendingIntent.getService(ProfileList.this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 30*1000, pintent);
//Create alarm manager
AlarmManager alarmMgr0 = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
//Create pending intent & register it to your alarm notifier class
Intent intent0 = new Intent(this, AlarmReciever.class);
PendingIntent pendingIntent0 = PendingIntent.getBroadcast(this, 0, intent0, 0);
//set timer you want alarm to work (here I have set it to 7.20pm)
Intent intent0 = new Intent(this, OldEntryRemover.class);
Calendar timeOff9 = Calendar.getInstance();
timeOff9.set(Calendar.HOUR_OF_DAY, 19);
timeOff9.set(Calendar.MINUTE, 20);
timeOff9.set(Calendar.SECOND, 0);
//set that timer as a RTC Wakeup to alarm manager object
alarmMgr0.set(AlarmManager.RTC_WAKEUP, timeOff0.getTimeInMillis(), pendingIntent0);
Then in your AlarmReciever class which is a broadcastReciever, under onRecieve method put your logic. This will take care of what ever the logic you want to handle when the time comes to 7.20 pm.
If you need to set multiple alarms, create another Calendar instance & set time values appropriately. You also need to create another instance for pendingIntent otherwise timers will overlap. Then set it to same alarmManager with new timer & pendingIntent.
You can read document from https://developer.android.com/training/scheduling/alarms.html
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
// Set the alarm to start at 8:30 a.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 30);
// setRepeating() lets you specify a precise custom interval--in this case,
// 20 minutes.
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 20, alarmIntent);
The following code should work fine and it starts the service # 7:40 PM every day. Also, if device shuts down then all your alarms get cancelled.
Make sure to set up all the alarms after BOOT is completed.
Intent slIntent = new Intent(this, x.class);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 19);
calendar.set(Calendar.MINUTE, 40);
calendar.set(Calendar.SECOND, 0);
PendingIntent slPendingIntent = PendingIntent.getService(this, 1, slIntent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager=(AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, slPendingIntent);
I tried a lot To Start Service on Time So I Have one solution like
Calculate the difference between current time and selected time
from date picker "Return Long timeMileSec = Milliseconds" Difference
after this create a handler inside it and Sleep if "Milliseconds" seconds
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
CreateService();
getActivity().startService(intentServiceObj);
}
}, timeMileSec);
// Below is the service Methods.
private void CreateService() {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.add(Calendar.DAY_OF_YEAR, year);
cal.set(Calendar.HOUR_OF_DAY, hourScreen);
cal.set(Calendar.MINUTE, minuteScreen);
// cal.setTimeInMillis(timeselectedmillisecond);
Intent intent = new Intent(getActivity(),
ServiceDailyLocationUpdate.class);
pintent = PendingIntent.getService(getActivity(), 0, intent, 0);
alarm = (AlarmManager) getActivity().getSystemService(
Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.HOUR, 86000 * 1000,
pintent);
}
// Return Differnce between two date
private long calculateDateDiffSecond(String firstDate, String secondDate) {
long numberOfDays = 0;
String dateStart = firstDate;
String dateStop = secondDate;
Date d1 = null;
Date d2 = null;
try {
d1 = sdfTime.parse(dateStart);
d2 = sdfTime.parse(dateStop);
// in milliseconds
long diff = d2.getTime() - d1.getTime();
long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000) % 24;
long diffDays = diff / (24 * 60 * 60 * 1000);
System.out.print(diffDays + " days, ");
System.out.print("Hours::" + diffHours + " hours, ");
System.out.print("HoursMinute::" + diffMinutes + " minutes, ");
System.out.print(diffSeconds + " seconds.");
numberOfDays = diffDays;
numberOfDays = diff;
} catch (Exception e) {
e.printStackTrace();
}
return numberOfDays;
}
codes above didn't work and below code worked for me.
month decreases 1.
and hours 0-11.
int day = ff.getGregorianDay() ;
int month = ff.getGregorianMonth() ;
int year = ff.getGregorianYear();
int hour = TimePicker1.getCurrentHour();
int minute = TimePicker1.getCurrentMinute();
Calendar cal_alarm = Calendar.getInstance();
cal_alarm.setTimeInMillis(System.currentTimeMillis() );
cal_alarm.set(Calendar.YEAR, year);
cal_alarm.set(Calendar.MONTH, month-1);
cal_alarm.set(Calendar.DAY_OF_MONTH, day);
cal_alarm.set(Calendar.AM_PM, Calendar.AM );
cal_alarm.set(Calendar.HOUR, hour);
if( hour >= 12){
cal_alarm.set(Calendar.AM_PM, Calendar.PM );
cal_alarm.set(Calendar.HOUR, hour-12);
}
cal_alarm.set(Calendar.MINUTE, minute );
cal_alarm.set(Calendar.SECOND, 0 );
Intent myIntent = new Intent(YadavariNewActivity.this, Alarm_Sag.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(YadavariNewActivity.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set( AlarmManager.RTC_WAKEUP, cal_alarm.getTimeInMillis(), pendingIntent);
Can you plese help me understand why my alarm won't fire :
I am trying to register a pending intent in oder to fire the alarm at 12:22 (current hour now) but the problem is that the alarm won't fire . I tried substituting the miliseconds from calendar.getTimeInMilies() with a hard-encoded time : 10 000 milies (10 seconds);
Not even that it had worked .
AlarmManager alarm_manager =(AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
calendar = Calendar.getInstance();
Intent intent = new Intent(MainActivity.this , AlarmManagerHelper.class);
PendingIntent register =PendingIntent.getActivity(getBaseContext(), 0, intent,0);
calendar.set(Calendar.YEAR,2014);
calendar.set(Calendar.MONTH, 10);
calendar.set(Calendar.DAY_OF_MONTH, 2);
calendar.set(Calendar.HOUR_OF_DAY,12);
calendar.set(Calendar.MINUTE,22);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);
alarm_manager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), register);
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
Log.d(TAG ,"Time format is :" +formatter.format(calendar.getTime()));
I also have the following permission set in the Manifest
android:name="android.permission.WAKE_LOCK"
And the output :
Time format is :02/11/2014 12:22:00
Try to use getBroadcast method instead of getActivity:
PendingIntent register =PendingIntent.getBroadcast(MainActivity.this, 0, intent,0);
Hope this helps.
I am using the alarmmanger class set method for setting the alarm. i
takes the input from user through timepicker dialog for setting the
alarm.alarm work fine when i set specific hour and minutes but
application is not following the am/pm logic. mean if i set the alarm
10:31 am while in system time is 10:30 pm then alarm invoke at 10:31 pm not at 10:30 am.anyone tell me whats the reason?
Calendar time = Calendar.getInstance();
time.set(Calendar.HOUR, hourOfDay);
time.set(Calendar.MINUTE, minute);
time.set(Calendar.SECOND, 5);
//time.set(Calendar.AM_PM);
AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
alarmMgr.set(AlarmManager.RTC_WAKEUP,time.getTimeInMillis(), pendingIntent);
you need to set the time with the time format using SimpleDateFormat or DateFormat class which will give the date based on the date-time string and get that value using getTime and set into the alarm in set method.
Or you can do another way get the alarm time and check whether it's am or pm and set this attribute into the calendar object with time and date and get millisecond value from the calendar set into the alarm.set() method.
Hope you get some idea/suggestion
Edit
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a"); // this is you format
Date d = sdf.parse("19/12/2011 03:47:00 pm"); // this string getting you from your timepicker
Intent myIntent = new Intent(youractivity.this, AlarmService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(youractivity.this, 0, myIntent, PendingIntent.FLAG_ONE_SHOT);
am.set(AlarmManager.RTC_WAKEUP, d.getTime(), pendingIntent);
Note: you need to put into the try and catch block parse part it may be cause error during parsing time
I used this method in setting alarm:
/**
* Set the Alarm
*
* #param context the activity context
* #param id the alarm ID for this app
* #param hour the alarm hour
* #param minute the alarm minute
* #param timeZone the timezone am = Calendar.AM or pm = Calendar.PM
*/
public static void setAlarm(Context context, int id, int hour, int minute, int timeZone) {
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.AM_PM, timeZone);
Intent intent = new Intent(context, PopupActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, id, intent, 0);
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 10, pIntent);
}
For the timezone parameter use either am = Calendar.AM or pm = Calendar.PM
Change the below line:
time.set(Calendar.HOUR, hourOfDay);
to
time.set(Calendar.HOUR_OF_DAY, hourOfDay);
I am trying to create the Alarms in my application using AlarmManager.
I am able to set multiple alarms, but if DATE parameter is added to Calender, the alarms are not at all triggered. Following is my code
Intent intent = new Intent(this, OneShotAlarm.class);
/*Pass the task row ID as the Unique ID for Pending Intent*/
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), (int) rowid , intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
calendar.clear();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, mHour);
calendar.set(Calendar.MINUTE, mMinute);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
long timeSet = calendar.getTimeInMillis();
alarmManager.set(AlarmManager.RTC_WAKEUP, timeSet, pendingIntent);
If I add the Date parameters to Calender as
calendar.add(Calendar.DAY_OF_MONTH, mDay);
calendar.add(Calendar.MONTH, mMonth);
calendar.add(Calendar.YEAR, mYear);
The alarms are not triggered. I have to schedule a event at a future date. Please suggest what I am missing. Thanks for the help!!
P.S. I am taking the date and time from Date & Time dialog picker
I have implement AlarmManager many times, Following technique will help you.
calculate your alarm time in milliseconds for example you want to set alarm after 10 minutes then 10*60*1000 millisecond after current time.
Add your calculated time in current millisecond
Example
long currentTime = System.currentTimeMillis();
long fireTime = 10 * 60 * 1000;
Intent ucintent = new Intent(getApplicationContext(),TimeAlarmReceiver.class);
ucintent.putExtra("isAlarm", true);
PendingIntent mTimeSlot = PendingIntent.getBroadcast(getApplicationContext(), (int)fireTime , ucintent, PendingIntent.FLAG_ONE_SHOT);
alarmManager.set(AlarmManager.RTC_WAKEUP,currentTime+ fireTime, mTimeSlot);
Above example works perfect.
Thank You,
Ketan's answer is good but there is an error.
long currentTime = System.currentTimeMillis();
long fireTime = 10 * 60 * 1000;
Intent ucintent = new Intent(getApplicationContext(),TimeAlarmReceiver.class);
ucintent.putExtra("isAlarm", true);
PendingIntent mTimeSlot = PendingIntent.getBroadcast(getApplicationContext(), (int) requestCode, ucintent, PendingIntent.FLAG_ONE_SHOT);
alarmManager.set(AlarmManager.RTC_WAKEUP,currentTime+ fireTime, mTimeSlot);
However you don't want "fireTime" in the PendingIntent. You should have a request code. Which is a code you create to identify your pending intents. It works in Ketan's case because he is always using the same time. But if you change the time, you will end up with two different intents.
see https://developer.android.com/reference/android/app/PendingIntent.html
I have searched a lot of places but couldnt find a clean sequential explanation of how to start a service (or if thats not possible then an activity) at a specific time daily using the AlarmManager??
I want to register several such alarms and triggering them should result in a service to be started. I'll be having a small piece of code in the service which can then execute and i can finish the service for good....
Calendar cal = Calendar.getInstance();
Calendar cur_cal = Calendar.getInstance();
cur_cal.setTimeInMillis(System.currentTimeMillis());
Date date = new Date(cur_cal.get(Calendar.YEAR), cur_cal.get(Calendar.MONTH), cur_cal.get(Calendar.DATE), 16, 45);
cal.setTime(date);
Intent intent = new Intent(ProfileList.this, ActivateOnTime.class);
intent.putExtra("profile_id", 2);
PendingIntent pintent = PendingIntent.getService(ProfileList.this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pintent);
System.out.println("The alarm set!!");
i tried this code to activate the alarm at 4.45... but its not firing the service... do i have to keep the process running??
M i doing anything wrong???
One more thing, my service gets perfectly executed in case i use the following code:
long firstTime = SystemClock.elapsedRealtime();
alarm.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 30*1000,pintent);
HI friends,
After a lot of researching and with reference from "Pentium10"'s question on the same topic i managed to get it working. Though i still cant understand why the "date" concept and the Calendar(non GregorianCalendar) object which i have mentioned in the question are not working correctly.
Calendar cur_cal = new GregorianCalendar();
cur_cal.setTimeInMillis(System.currentTimeMillis());//set the current time and date for this calendar
Calendar cal = new GregorianCalendar();
cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
cal.set(Calendar.HOUR_OF_DAY, 18);
cal.set(Calendar.MINUTE, 32);
cal.set(Calendar.SECOND, cur_cal.get(Calendar.SECOND));
cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));
Intent intent = new Intent(ProfileList.this, IntentBroadcastedReceiver.class);
PendingIntent pintent = PendingIntent.getService(ProfileList.this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 30*1000, pintent);
//Create alarm manager
AlarmManager alarmMgr0 = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
//Create pending intent & register it to your alarm notifier class
Intent intent0 = new Intent(this, AlarmReciever.class);
PendingIntent pendingIntent0 = PendingIntent.getBroadcast(this, 0, intent0, 0);
//set timer you want alarm to work (here I have set it to 7.20pm)
Intent intent0 = new Intent(this, OldEntryRemover.class);
Calendar timeOff9 = Calendar.getInstance();
timeOff9.set(Calendar.HOUR_OF_DAY, 19);
timeOff9.set(Calendar.MINUTE, 20);
timeOff9.set(Calendar.SECOND, 0);
//set that timer as a RTC Wakeup to alarm manager object
alarmMgr0.set(AlarmManager.RTC_WAKEUP, timeOff0.getTimeInMillis(), pendingIntent0);
Then in your AlarmReciever class which is a broadcastReciever, under onRecieve method put your logic. This will take care of what ever the logic you want to handle when the time comes to 7.20 pm.
If you need to set multiple alarms, create another Calendar instance & set time values appropriately. You also need to create another instance for pendingIntent otherwise timers will overlap. Then set it to same alarmManager with new timer & pendingIntent.
You can read document from https://developer.android.com/training/scheduling/alarms.html
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
// Set the alarm to start at 8:30 a.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 30);
// setRepeating() lets you specify a precise custom interval--in this case,
// 20 minutes.
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 20, alarmIntent);
The following code should work fine and it starts the service # 7:40 PM every day. Also, if device shuts down then all your alarms get cancelled.
Make sure to set up all the alarms after BOOT is completed.
Intent slIntent = new Intent(this, x.class);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 19);
calendar.set(Calendar.MINUTE, 40);
calendar.set(Calendar.SECOND, 0);
PendingIntent slPendingIntent = PendingIntent.getService(this, 1, slIntent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager=(AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, slPendingIntent);
I tried a lot To Start Service on Time So I Have one solution like
Calculate the difference between current time and selected time
from date picker "Return Long timeMileSec = Milliseconds" Difference
after this create a handler inside it and Sleep if "Milliseconds" seconds
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
CreateService();
getActivity().startService(intentServiceObj);
}
}, timeMileSec);
// Below is the service Methods.
private void CreateService() {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.add(Calendar.DAY_OF_YEAR, year);
cal.set(Calendar.HOUR_OF_DAY, hourScreen);
cal.set(Calendar.MINUTE, minuteScreen);
// cal.setTimeInMillis(timeselectedmillisecond);
Intent intent = new Intent(getActivity(),
ServiceDailyLocationUpdate.class);
pintent = PendingIntent.getService(getActivity(), 0, intent, 0);
alarm = (AlarmManager) getActivity().getSystemService(
Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.HOUR, 86000 * 1000,
pintent);
}
// Return Differnce between two date
private long calculateDateDiffSecond(String firstDate, String secondDate) {
long numberOfDays = 0;
String dateStart = firstDate;
String dateStop = secondDate;
Date d1 = null;
Date d2 = null;
try {
d1 = sdfTime.parse(dateStart);
d2 = sdfTime.parse(dateStop);
// in milliseconds
long diff = d2.getTime() - d1.getTime();
long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000) % 24;
long diffDays = diff / (24 * 60 * 60 * 1000);
System.out.print(diffDays + " days, ");
System.out.print("Hours::" + diffHours + " hours, ");
System.out.print("HoursMinute::" + diffMinutes + " minutes, ");
System.out.print(diffSeconds + " seconds.");
numberOfDays = diffDays;
numberOfDays = diff;
} catch (Exception e) {
e.printStackTrace();
}
return numberOfDays;
}
codes above didn't work and below code worked for me.
month decreases 1.
and hours 0-11.
int day = ff.getGregorianDay() ;
int month = ff.getGregorianMonth() ;
int year = ff.getGregorianYear();
int hour = TimePicker1.getCurrentHour();
int minute = TimePicker1.getCurrentMinute();
Calendar cal_alarm = Calendar.getInstance();
cal_alarm.setTimeInMillis(System.currentTimeMillis() );
cal_alarm.set(Calendar.YEAR, year);
cal_alarm.set(Calendar.MONTH, month-1);
cal_alarm.set(Calendar.DAY_OF_MONTH, day);
cal_alarm.set(Calendar.AM_PM, Calendar.AM );
cal_alarm.set(Calendar.HOUR, hour);
if( hour >= 12){
cal_alarm.set(Calendar.AM_PM, Calendar.PM );
cal_alarm.set(Calendar.HOUR, hour-12);
}
cal_alarm.set(Calendar.MINUTE, minute );
cal_alarm.set(Calendar.SECOND, 0 );
Intent myIntent = new Intent(YadavariNewActivity.this, Alarm_Sag.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(YadavariNewActivity.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set( AlarmManager.RTC_WAKEUP, cal_alarm.getTimeInMillis(), pendingIntent);