Java 如何根据android数据库中保存的日期和时间设置报警
我使用自定义事件在数据库中存储数据,我有主题、描述、日期和时间。是否可以根据数据库中插入的日期和时间设置报警?我试图检索所有日期和时间,以匹配当前日期和确切时间。你知道怎么做吗?是的,你可以在android中使用BroadcastReceiver类。您必须搜索并阅读它。首先您需要从db获取日期Java 如何根据android数据库中保存的日期和时间设置报警,java,android,sqlite,events,android-alarms,Java,Android,Sqlite,Events,Android Alarms,我使用自定义事件在数据库中存储数据,我有主题、描述、日期和时间。是否可以根据数据库中插入的日期和时间设置报警?我试图检索所有日期和时间,以匹配当前日期和确切时间。你知道怎么做吗?是的,你可以在android中使用BroadcastReceiver类。您必须搜索并阅读它。首先您需要从db获取日期 String dateTime = row.getString(row.getColumnIndexOrThrow(COLUMN_INDEX)); 这将返回一个字符串,对其进行解析并重新格式化为您的本地
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);
}
Calendar cal=Calendar.getInstance();
校准设定时间(日期);
当前日历=Calendar.getInstance();
如果(校准比较)到(当前)问:是否可以根据数据库中插入的日期和时间设置警报?
A:是的。我只想设置一个基本警报,只有当用户在应用程序中并且到达sqlite数据库的到期日期时才会触发。在这种情况下,不需要AlarmManager正确吗?显然,只需要使用一个带有计时器和线程的处理程序。但是我没有看到任何关于stackoverflow的处理程序示例。请给出建议。