Everyone is here saying use setExact
for the API level 19 and higher but couldn't find how to set the repeating time in it so that it get repeated again at specified time.
So please tell me how to set the repeating time in setExact
method?
here is my code
Calendar calendar2,calendar,cal,cal2;
calendar = new GregorianCalendar();
calendar2 = new GregorianCalendar();
calendar2.setTimeInMillis(System.currentTimeMillis());
calendar.setTimeInMillis(System.currentTimeMillis());
cal = new GregorianCalendar();
cal2 = new GregorianCalendar();
cal.add(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR));
cal.set(Calendar.HOUR_OF_DAY, 10);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, calendar.get(Calendar.SECOND));
cal.set(Calendar.MILLISECOND, calendar.get(Calendar.MILLISECOND));
cal.set(Calendar.DATE, calendar.get(Calendar.DATE));
cal.set(Calendar.MONTH, calendar.get(Calendar.MONTH));
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Intent it = new Intent(MainActivity.this,Start_service_alarm.class);
it.putExtra(Start_service_alarm.ACTION, Start_service_alarm.ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 987654321, it, 0);
if(android.os.Build.VERSION.SDK_INT<19)
{
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}else
{
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
}
cal2.add(Calendar.DAY_OF_YEAR, calendar2.get(Calendar.DAY_OF_YEAR));
cal2.set(Calendar.HOUR_OF_DAY, 20);
cal2.set(Calendar.MINUTE, 0);
cal2.set(Calendar.SECOND, calendar2.get(Calendar.SECOND));
cal2.set(Calendar.MILLISECOND, calendar2.get(Calendar.MILLISECOND));
cal2.set(Calendar.DATE, calendar2.get(Calendar.DATE));
cal2.set(Calendar.MONTH, calendar2.get(Calendar.MONTH));
AlarmManager alarmManager2 = (AlarmManager)getSystemService(ALARM_SERVICE);
Intent itIntent = new Intent(MainActivity.this,Stop_service_alarm.class);
itIntent.putExtra("ACTION_STOP", Stop_service_alarm.ACTION_STOP);
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(MainActivity.this, 123456789, itIntent, 0);
if(android.os.Build.VERSION.SDK_INT<19)
{
alarmManager2.setRepeating(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent2);
}else
{
alarmManager2.setExact(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), pendingIntent2);
}