当我选中复选框时,我的警报正在运行。它在选定的时间内不起作用

时间:2016-07-23 19:04:11

标签: android checkbox alarmmanager android-alarms

在这里我使用Edittext选择时间并选择复选框来执行闹钟。我选择的时间不起作用,当我选中复选框时它开始响铃

ca = Calendar.getInstance();
            mHour = ca.get(Calendar.HOUR_OF_DAY);
            //timeET.getCurrentHour();
            mMinute = ca.get(Calendar.MINUTE);
            TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                    timeET.setText(hourOfDay +" : "+minute);
                }
            },mHour,mMinute,false);
            timePickerDialog.show();
            fixTime = timeET.getText().toString();
    }


}

View.OnClickListener checkboxClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        boolean checked = ((CheckBox) v).isChecked();

        if (checked){
            switch (v.getId()){
                case R.id.alarm:
                    Intent myIntent = new Intent(CountryEdit.this,AlarmReceiver.class);
                    //An pendingIntent specifies an action to take in the future
                    pendingIntent = PendingIntent.getBroadcast(CountryEdit.this,0,myIntent,0);
                    //set Alarm Time
                    alarmManager.set(AlarmManager.RTC,ca.getTimeInMillis(),pendingIntent);
                    break;
                case R.id.remainder:
                    break;

            }
        }else{
            alarmManager.cancel(pendingIntent);
        }

这是我的AlarmReceiver类,我告诉系统响铃选择时间。

 @Override
public void onReceive(Context context, Intent intent) {
    //This will update the UT with message
    try{
        CountryEdit inst = CountryEdit.instance();

        //This will sound my Alarm once if i wish to sound my alarm in loop continuously then use MediaPlayer
        Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);

        if (alarmUri == null){
            alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        }

        Ringtone ringtone = RingtoneManager.getRingtone(context,alarmUri);
        ringtone.play();

    }catch (Exception ex){

    }

}

我不知道问题出在哪里。有没有人请帮助我。我是初学者。

1 个答案:

答案 0 :(得分:0)

这是因为当您将复选框设置为选中时,您正在调用ca.getTimeInMillis()。它会在您触摸复选框的当前时间内将闹钟设置为正确。

您可能希望将ca.getTimeInMillis()更改为fixTime

注意:假设fixTime是您希望闹铃响起的时间变量。