Android 如何根据我的 sqlite 数据库中设置的时间设置警报?
Android how can I set an alarm based on the time set in my sqlite database?
我有一个应用程序可以从名为任务的网络服务接收数据,然后将任务保存到我的 SQLite 数据库中。我接下来要做的是查询这些任务,然后根据设置的触发时间设置警报。假设我有一个任务名称 "feed" 并且要触发的任务时间是“09:00 AM”(我的时间使用 'h:i A' 格式)。如何将此时间格式转换为 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();
}
String date_string = "09:00 AM";
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");
try {
Date date = sdf.parse(date_string);
long milliseconds = date.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
使用毫秒。
试试这个,看看它是否适合你
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);
在特定时间更新设置
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 30);
long alarmInTwoMinutes = calendar.getTimeInMillis();
要重复,可以使用
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmInTwoMinutes, pendingIntent)
我有一个应用程序可以从名为任务的网络服务接收数据,然后将任务保存到我的 SQLite 数据库中。我接下来要做的是查询这些任务,然后根据设置的触发时间设置警报。假设我有一个任务名称 "feed" 并且要触发的任务时间是“09:00 AM”(我的时间使用 'h:i A' 格式)。如何将此时间格式转换为 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();
}
String date_string = "09:00 AM";
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");
try {
Date date = sdf.parse(date_string);
long milliseconds = date.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
使用毫秒。
试试这个,看看它是否适合你
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);
在特定时间更新设置
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 30);
long alarmInTwoMinutes = calendar.getTimeInMillis();
要重复,可以使用
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmInTwoMinutes, pendingIntent)