我使用自定义事件将数据存储在数据库中,我有主题,描述,日期和时间。是否可以根据插入数据库的日期和时间设置警报?我正在尝试检索所有日期和时间以匹配当天和确切时间。你知道怎么做吗?
答案 0 :(得分:0)
是的,你可以在android中使用BroadcastReceiver类来实现。你必须搜索并阅读它。
答案 1 :(得分:0)
首先,您需要从db获取日期。
String dateTime = row.getString(row.getColumnIndexOrThrow(COLUMN_INDEX));
这样,返回一个字符串,解析它并重新格式化为您的本地格式和时区:
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);
}
现在使用以下代码设置闹钟
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);
}