Alarmmanager不显示通知

时间:2018-10-26 08:26:23

标签: android android-activity alarmmanager android-notifications android-alarms

当我从日历中选择日期到警报管理器时,我将警报管理器设置为多个警报,警报管理器应警报2次。在时间24 HR中,它是19.00和7.00。 运行代码后,我添加日期26-10-2018它显示通知1时间。但是在早上7.00它没有任何通知。如果时间到了,我又添加了警报,那么它将显示通知。如何解决?

这是用于从日历中选择日期添加到数据库中的代码。

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

                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.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);

                        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) {

                        final int rq_code = (int) System.currentTimeMillis();

                        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_RQCODE, rq_code);
                        cv.put(DBHelper.COL_DATE, mDisplayDate.getText().toString());

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


           /********************** Alarm Here **************************/

                        DateAlarm(rq_code);

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

                            }
                        }).show();

                return true;

这是警报代码

public void DateAlarm(int rq_Code){

        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,19);  //HOUR
        next1.set(Calendar.MINUTE, 0);       //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,7);  //HOUR
        next2.set(Calendar.MINUTE, 0);       //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);
        Log.e("TAG", "Request Code : " + rq_Code);

        Intent myIntent = new Intent(CarOptionActivity.this, AlarmCar.class);
        //PendingIntent pendingIntent = PendingIntent.getBroadcast(DriverLicenseActivity.this,0,myIntent,0);
        PendingIntent pendingIntent1 = PendingIntent.getBroadcast(getApplicationContext(), rq_Code, myIntent, 0);
        PendingIntent pendingIntent2 = PendingIntent.getBroadcast(getApplicationContext(), rq_Code+1, 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);
    }

这是错误日志。

  E/TAG: Parsing Date : Thu Oct 25 19:00:00 GMT+07:00 2018
  E/TAG: Parsing Date : Fri Oct 26 07:00:00 GMT+07:00 2018
  E/TAG: Request Code : -1367691547
  E/TAG: Parsing Date : Sat Oct 27 19:00:00 GMT+07:00 2018
  E/TAG: Parsing Date : Sun Oct 28 07:00:00 GMT+07:00 2018
  E/TAG: Request Code : -1412631850 
  19120-19120/? E/ClearRunningAppService: onStartCommand intent == null
  E/TAG: Parsing Date : Fri Oct 26 19:00:00 GMT+07:00 2018 
  E/TAG: Parsing Date : Sat Oct 27 07:00:00 GMT+07:00 2018
  E/TAG: Request Code : -1412637200

1 个答案:

答案 0 :(得分:0)

如果您使用的是通知类及其方法,则看不到。使用Notification和Notificationmanager时,请添加代码以进行更多检查。谢谢