我正在做一个警报应用程序,根据日期选择器的日期和时间,它必须播放警报。
我编写的守则
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, day);
cal.set(Calendar.HOUR, hour);
cal.set(Calendar.MINUTE, min);
//Create a new PendingIntent and add it to the AlarmManager
Intent intent = new Intent(this, AlarmReceiverActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am =
(AlarmManager)getSystemService(Activity.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
pendingIntent);
除了我们硬编码像cal.add(Calendar.SECOND,5)之类的值之外,在日历中设置值。它播放闹钟。如果有人请帮助我找到问题。
答案 0 :(得分:0)
您可以尝试运行此代码块,因为我使用它。
String str_date = "23:59:59";
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
java.util.Date stringToDate = sdf.parse(str_date);
long start_time = stringToDate.getTime();
Intent __intent__ = new Intent(this, CleanService.class);
PendingIntent __p_intent__ = PendingIntent.getService
(getApplicationContext(), 0, __intent__, 0);
long interval_time = 86400000;
AlarmManager alarm_manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm_manager.setRepeating(AlarmManager.RTC_WAKEUP,
start_time, interval_time, __p_intent__);