为什么我的AlarmManager不正确?

时间:2016-05-26 04:49:13

标签: android

我尝试编写一个报警应用程序,当我选择系统时间后,它运行良好。但是当我在系统时间之前选择另一个时间。它会立即报警,我无法理解:(请帮助我。我使用TimepickerDialog来选择时间。 这是选择时间码。(点击textview)

time.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //savePrefs();
            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            int minute = calendar.get(Calendar.MINUTE);
            TimePickerDialog mTimePicker;
            mTimePicker = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
                    timePicker.setIs24HourView(true);
                    calendar.set(Calendar.HOUR_OF_DAY, selectedHour);
                    calendar.set(Calendar.MINUTE, selectedMinute);
                    calendar.set(Calendar.SECOND, 0);
                    get_hour = selectedHour;
                    get_min = selectedMinute;
                    millis = calendar.getTimeInMillis();
                    time.setText( selectedHour + " : " + selectedMinute);
                }
            }, hour, minute, true);
            mTimePicker.setTitle("Set Alarm");
            onoff_alarm.setChecked(false);
            mTimePicker.show();

        }
    });

这是我的onoff togglebutton

onoff_alarm.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(onoff_alarm.isChecked()){
                my_intent.putExtra("extra", "alarm on");
                Toast.makeText(getApplicationContext(), " Alarm On :  " + get_hour + " h " + get_min + " m ", Toast.LENGTH_SHORT).show();
                pending_intent = PendingIntent.getBroadcast(MainActivity.this, 0, my_intent, PendingIntent.FLAG_UPDATE_CURRENT);
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, millis, AlarmManager.INTERVAL_DAY,pending_intent);
            }
            else {
                my_intent.putExtra("extra","alarm off");
                Toast.makeText(getApplicationContext(), "Alarm Off !", Toast.LENGTH_SHORT).show();
                alarmManager.cancel(pending_intent);
                sendBroadcast(my_intent);
            }
        }
    });

1 个答案:

答案 0 :(得分:1)

警报管理器的目的是在将来的某个时间之后安排一些任务。为什么要在过去触发警报,即在系统时间之前? 我认为代码工作正常。 设置系统时间之前的时间会立即触发警报,因为时间已经过去了。