在Android中安排每日重复闹钟

时间:2017-07-20 16:51:32

标签: android alarmmanager android-alarms

我想在android中安排一个重复闹钟,我能够部分完成这个闹钟只会触发一次而且永远不会每天重复,这是我的代码片段

    /* Schedule the alarm based on user preferences */
public void scheduleAlarm(Context context) {
    String alarmPref;

    manager = AlarmManagerProvider.getAlarmManager(context);

    String keyReminder = context.getString(R.string.pref_key_reminder);
    String keyAlarm = context.getString(R.string.pref_key_alarm);
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);

    boolean enabled = preferences.getBoolean(keyReminder, false);

    //Intent to trigger
    Intent intent = new Intent(context, ReminderService.class);
    operation = PendingIntent
            .getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);


    if (enabled) {
        Calendar calendar = Calendar.getInstance();

        alarmPref = preferences.getString(keyAlarm, "12:00");

        calendar.setTimeInMillis(System.currentTimeMillis());

        String[] str = alarmPref.split(":");
        int hourDay = Integer.parseInt(str[0]);
        int minute = Integer.parseInt(str[1]);

        calendar.set(Calendar.HOUR_OF_DAY, hourDay);
        calendar.set(Calendar.MINUTE, minute);

        if (Calendar.getInstance().after(calendar)) {
            calendar.add(Calendar.DATE, 1);
        }

        Log.d(TAG, "Scheduling quiz reminder alarm" + hourDay);
        Log.d(TAG, "Scheduling quiz reminder alarm" + minute);

        manager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                calendar.getTimeInMillis(), 24 * 60 * 60 * 1000, operation);
    } else {
        Log.d(TAG, "Disabling quiz reminder alarm");
        manager.cancel(operation);
    }
}

我想在选定的日子里每天重复这个闹钟接收器。目前它只工作一次,并且从不在每天的确切日重复

0 个答案:

没有答案