Why RepeatingAlarm Only Runs For 1 Time Only
I am working on repeating alarm . problem is that my repeat alarm only runs for one time only on set time but not repeat on that time next day. If it is 5/28/2015 and time is 1:40P
Solution 1:
call at every day at night : 12 AM
Calendar currentCalendar = Calendar.getInstance();
Calendar todaysCalendar = Calendar.getInstance();
todaysCalendar.setTime(getDate());
Calendar nextDayCalandar = Calendar.getInstance();
nextDayCalandar.setTimeInMillis(todaysCalendar.getTimeInMillis()
+ (3600000 * 24));
long time = Calendar.getInstance().getTimeInMillis()
+ (nextDayCalandar.getTimeInMillis() - currentCalendar.getTimeInMillis());
Intent myIntent = new Intent(HomeActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(HomeActivity.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT)
{
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, 3600000 * 24, pendingIntent);
}
else
{
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, time, 3600000 * 24, pendingIntent);
}
supportive method
@SuppressLint("SimpleDateFormat")
private Date getDate()
{
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
try
{
return format.parse(format.format(new Date()));
}
catch (ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return new Date();
}
Post a Comment for "Why RepeatingAlarm Only Runs For 1 Time Only"