I am using a custom event to store data in the database, I have subject, description, date, and time. Is it possible to set an alarm based on the date and time inserted in the database? I am trying to retrieve all of dates and time to match up on the current day and exact time. Do you have any idea how to do this?
Asked
Active
Viewed 2,197 times
2
-
Q: `Is it possible to set an alarm based on the date and time inserted in the database?` A: **Yes, it is**. – Phantômaxx Sep 10 '16 at 17:42
2 Answers
1
First you need to get date from db.
String dateTime = row.getString(row.getColumnIndexOrThrow(COLUMN_INDEX));
This, returns a string, parse it and reformat to your local format and time zone:
DateFormat yourDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
date = yourDateFormat.parse(dateTime);
} catch (ParseException e) {
Log.e(TAG, "Parsing date time failed", e);
}
now set a alarm using below code
Calendar cal = Calendar.getInstance();
cal.setTime(date);
Calendar current = Calendar.getInstance();
if(cal.compareTo(current) <= 0)
{
//The set Date/Time already passed
Toast.makeText(getApplicationContext(), "Invalid Date/Time", Toast.LENGTH_LONG).show();
}
else{
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
}

CodingRat
- 1,934
- 3
- 23
- 43
-
I want to just set up a basic alarm that only triggers when the user is in the app and a due date from my sqlite database is reached. In that case, there is no need for an AlarmManager correct? Apparently all that is needed is use of a Handler with Timer and Thread. Yet I don't see any Handler examples on stackoverflow. Please advise. – AJW Feb 08 '18 at 04:39
0
Yes, you can do it by using BroadcastReceiver class in android. You must search and read it.

nabia saroosh
- 399
- 1
- 14
- 36