报警起火太早了

时间:2017-11-11 21:31:33

标签: android alarmmanager

我正在试图通知警报。

我面临的问题是它过早地发出通知......

我真的需要从日期和时间选择器中获取日期和时间。

我不知道我的代码有什么问题。这是我的代码:

MainActivity.java

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fire = findViewById(R.id.fire);
        choose = findViewById(R.id.choose);

        choose.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onClick(View view) {
                final Calendar cal = getInstance();
                int year = cal.get(YEAR);
                int month = cal.get(MONTH);
                int day = cal.get(DAY_OF_MONTH);

                final DatePickerDialog dialog = new DatePickerDialog(
                        MainActivity.this,
                        R.style.Theme_AppCompat_Light,
                        mDataSetListener,
                        year,month,day
                );

                dialog.setOnDateSetListener(new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker datePicker, final int year, final int day, final int month) {
                        final Calendar calTime = getInstance();
                        int hour = calTime.get(Calendar.HOUR_OF_DAY);
                        int minute = calTime.get(Calendar.MINUTE);

                        final String datum = day + "." + month + "." + year;

                        TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this,R.style.Theme_AppCompat_Light, new TimePickerDialog.OnTimeSetListener() {

                            @Override
                            public void onTimeSet(TimePicker timePicker, int hour, int minute) {

                                final Calendar cal_new = getInstance();
                                cal_new.set(Calendar.YEAR,year);
                                cal_new.set(Calendar.MONTH,month-1);
                                cal_new.set(Calendar.DAY_OF_MONTH,day);

                                cal_new.set(Calendar.HOUR_OF_DAY,hour);
                                cal_new.set(Calendar.MINUTE,minute);


                                Toast.makeText(MainActivity.this,day + " " + month + " " + year + " " + hour + ":" + minute, Toast.LENGTH_SHORT).show();
                                AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
                                Intent in = new Intent(MainActivity.this,Alarm.class);
                                PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this,0,in,0);
                                alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal_new.getTimeInMillis(),pi);
                            }
                        },hour,minute,true);
                        timePickerDialog.show();
                    }
                });
                dialog.show();

            }
        });

Alarm.java (BroadCastReceiver)

public class Alarm extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Yeah", Toast.LENGTH_SHORT).show();
        try {
            Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            Ringtone r = RingtoneManager.getRingtone(context, notification);
            r.play();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

我尝试了一些解决方案,但没有用。

谢谢!

2 个答案:

答案 0 :(得分:1)

问题是您要设置month-1期望从1到12获取值。对于与Calendar的兼容性,onDateChanged的month已经是0到11。

无需修改获得的月份,将其设置为警报管理器,如下所示:

cal_new.set(Calendar.MONTH, month);

检查DatePickerDiaglo doc.

答案 1 :(得分:0)

您的cal_new可能会使用hour以外的minuteonTimeSet()变量进行初始化。尝试重命名参数。