0

我正在做一个闹钟应用程序,根据日期选择器中的日期和时间,它必须播放闹钟。

我写的代码

        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); 它播放警报..如果有人请帮我找到问题。

4

1 回答 1

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__);
于 2013-02-18T12:23:43.380 回答