I have an app which receives data from web service called tasks, tasks are then saved to my SQLite database. What I want to do next is query those tasks and then set an alarm based on the time they are set to be triggered. Let's say I have a task name "feed" and the time of the task to be triggered is at '09:00 AM'(I am using 'h:i A' format for my time). How can I convert this time format to something that can be read by the alarmManager?
public void setAlarm(int uniqueid, int duration) {
Intent intent = new Intent(this, AlarmReceiver.class);
intent.setAction(AlarmReceiver.ACTION_ALARM_RECEIVER);//my custom string action name
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, uniqueid, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (duration * 1000), pendingIntent);
Toast.makeText(this, "Alarm set for " + uniqueid, Toast.LENGTH_LONG).show();
}
Try this and see if it works for you
Date mDate = new Date();
Calendar mCalendar = Calendar.getInstance();
mCalendar.setTime(mDate);
mCalendar.add(Calendar.MINUTE, 2);
long alarmInTwoMinutes = mCalendar.getTimeInMillis();
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmInTwoMinutes, pendingIntent);
Update to setting at a specific time
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 30);
long alarmInTwoMinutes = calendar.getTimeInMillis();
For repeating, you can use
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmInTwoMinutes, pendingIntent)