警报管理器在指定日期不工作

时间:2018-10-21 19:53:42

标签: android alarmmanager android-notifications

我想警报通知2次。我创建了警报管理器,但是单击“确定”按钮后立即显示通知。

这是警报代码。

public void DateAlarm(){

        String p = mDisplayDate.getText().toString();
        String[] parts = p.split("-");

        String day = parts[0];
        String month = parts[1];
        String year = parts[2];
        Calendar next1 = Calendar.getInstance();
        next1.set(Calendar.YEAR, Integer.parseInt(year));//year...
        next1.set(Calendar.MONTH, Integer.parseInt(month)-1);  //first month is 0!!! January is zero!!!
        next1.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day)-1);  //1-31

        next1.set(Calendar.HOUR_OF_DAY,0);  //HOUR
        next1.set(Calendar.MINUTE, 54);       //MIN
        next1.set(Calendar.SECOND, 0);       //SEC

        Calendar next2 = Calendar.getInstance();
        next2.set(Calendar.YEAR, Integer.parseInt(year));//year...
        next2.set(Calendar.MONTH, Integer.parseInt(month)-1);  //first month is 0!!! January is zero!!!
        next2.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day));  //1-31

        next2.set(Calendar.HOUR_OF_DAY,11);  //HOUR
        next2.set(Calendar.MINUTE, 35);       //MIN
        next2.set(Calendar.SECOND, 0);       //SEC

        Calendar now = Calendar.getInstance();
        if(now.after(next1)) {
            next1.add(Calendar.HOUR_OF_DAY, 24);
        }
        if(now.after(next2)) {
            next2.add(Calendar.HOUR_OF_DAY, 24);
        }

        // next.add(Calendar.DAY_OF_YEAR, -1);
        Date newDate1 = next1.getTime();
        Log.e("TAG", "Parsing Date : " + newDate1);
        Date newDate2 = next2.getTime();
        Log.e("TAG", "Parsing Date : " + newDate2);

        Intent myIntent = new Intent(CarOptionActivity.this, AlarmCar.class);
        //PendingIntent pendingIntent = PendingIntent.getBroadcast(DriverLicenseActivity.this,0,myIntent,0);
        PendingIntent pendingIntent1 = PendingIntent.getBroadcast(getApplicationContext(), 2, myIntent, 0);
        PendingIntent pendingIntent2 = PendingIntent.getBroadcast(getApplicationContext(), 3, myIntent, 0);

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, next1.getTimeInMillis(), DateUtils.DAY_IN_MILLIS,pendingIntent1);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, next2.getTimeInMillis(), DateUtils.DAY_IN_MILLIS,pendingIntent2);
    }

Log.e像这样显示正确的日期时间。

  

10-22 01:07:59.510 31490-31490 /? E / TAG:解析日期:9月27日星期四   01:09:00 GMT + 07:00 2018 10-22 01:07:59.510 31490-31490 /? E / TAG:   解析日期:Fri Sep 28 11:35:00 GMT + 07:00 2018

这是从日历获取日期到警报的代码。

switch (item.getItemId()) {
    case R.id.car:

        dialogBody = inflater.inflate(R.layout.dlg_option, null);
        mDisplayDate = (TextView) dialogBody.findViewById(R.id.txt_next_time);
        sdf = new SimpleDateFormat( "dd-MM-yyyy" );
        mDisplayDate.setText( sdf.format( new Date() ));

        mDisplayDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar cal = Calendar.getInstance();
                int year = cal.get(Calendar.YEAR);
                int month = cal.get(Calendar.MONTH);
                int day = cal.get(Calendar.DAY_OF_MONTH);


                DatePickerDialog dialog = new DatePickerDialog(CarOptionActivity.this,
                        android.R.style.Theme_DeviceDefault_Dialog_MinWidth,
                        mDateSetListener,
                        year, month, day);

                dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                dialog.show();


            }
        });

        mDateSetListener = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {

                //month = month + 1;
                String date = dayOfMonth + "-" + month + "-" + year;
                mDisplayDate.setText(date);
            }
        };

        builder.setView(dialogBody);


        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

                ContentValues cv = new ContentValues();
                cv.put(DBHelper.COL_VEHICLE_TYPE, 0);
                cv.put(DBHelper.COL_OPTION_NAME, 1);
                cv.put(DBHelper.COL_DISTANCE, 0);
                cv.put(DBHelper.COL_DATE, mDisplayDate.getText().toString());

                db.insert(DBHelper.TABLE_NAME, null, cv);
                cursor.requery();
                getData();

                ///////////////// DateAlarm was call here ///////////////
                DateAlarm();

            }
        })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(CarOptionActivity.this, "car cancel",
                                Toast.LENGTH_SHORT).show();
                        dialogInterface.dismiss();

                    }
                }).show();

        return true;
在日历中选择日期之后,调用了

Function DateAlarm()函数。但是,当我单击确定按钮时,警报管理器立即显示通知。没有在指定日期显示。如何解决?

0 个答案:

没有答案