如何在Android中一起在两个不同的时间设置警报通知

时间:2019-05-15 11:02:07

标签: android service

我已经创建了一个应用程序,用户可以在其中选择签入和签出时间,即使关闭了该应用程序,我也希望向用户显示这两个时间的状态栏通知。

我使用服务来按时触发通知,当我单独呼叫它们时会显示通知,但是当我同时为两者设置警报时,通知仅在结帐时弹出,但称其为签入时间。我不知道原因。

我绑定服务的班级

RecordId    Food            Drink
----------- --------------- ---------------
1           Apple           Water
2           Banana          Orange Juice
2           Steak           Beer

弹出通知的我的服务类

    checkin.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            calendar = Calendar.getInstance();
            currentHour = calendar.get(Calendar.HOUR_OF_DAY);
            currentMinute = calendar.get(Calendar.MINUTE);

            timePickerDialog = new TimePickerDialog(Reservation.this, new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker timePicker, int hourOfDay, int minutes) {
                    selectedhour=hourOfDay;
                    selectedminute=minutes;
                    if (hourOfDay >= 12) {
                        amPm = "PM";
                    } else {
                        amPm = "AM";
                    }
                    checkin.setText(String.format("%02d:%02d", hourOfDay, minutes) + amPm);
                }
            }, currentHour, currentMinute, false);

            timePickerDialog.show();

        }
    });
    checkout.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            calendar = Calendar.getInstance();
            currentHour = calendar.get(Calendar.HOUR_OF_DAY);
            currentMinute = calendar.get(Calendar.MINUTE);

            timePickerDialog = new TimePickerDialog(Reservation.this, new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker timePicker, int hourOfDay, int minutes) {
                    selectedcouthour=hourOfDay;
                    selectedcoutmin=minutes;
                    if (hourOfDay >= 12) {
                        amPm = "PM";
                    } else {
                        amPm = "AM";
                    }
                    checkout.setText(String.format("%02d:%02d", hourOfDay, minutes) + amPm);
                }
            }, currentHour, currentMinute, false);

            timePickerDialog.show();

        }
    });
  .....
  private void setnotification() {
    Calendar calendar = Calendar.getInstance();

    int thisYear = calendar.get(Calendar.YEAR);
    Log.d("nn", "# thisYear : " + thisYear);

    int thisMonth = calendar.get(Calendar.MONTH);
    Log.d("nn", "@ thisMonth : " + thisMonth);

    int thisDay = calendar.get(Calendar.DAY_OF_MONTH);
    Log.d("nn", "$ thisDay : " + thisDay);
   // Calendar c = Calendar.getInstance();
    calendar.set(thisYear, thisMonth, thisDay);
    calendar.set(Calendar.HOUR_OF_DAY, selectedhour);
    calendar.set(Calendar.MINUTE, selectedminute);
    calendar.set(Calendar.SECOND, 0);
    Calendar calendar1 = Calendar.getInstance();

    int thisYear1 = calendar1.get(Calendar.YEAR);
    Log.d("nn", "# thisYear : " + thisYear);

    int thisMonth1 = calendar1.get(Calendar.MONTH);
    Log.d("nn", "@ thisMonth : " + thisMonth);

    int thisDay1 = calendar1.get(Calendar.DAY_OF_MONTH);
    Log.d("nn", "$ thisDay : " + thisDay);
    // Calendar c = Calendar.getInstance();
    calendar1.set(thisYear, thisMonth, thisDay);
    calendar1.set(Calendar.HOUR_OF_DAY, selectedcouthour);
    calendar1.set(Calendar.MINUTE, selectedcoutmin);
    calendar1.set(Calendar.SECOND, 0);
    //Calendar c1 = Calendar.getInstance();
    // Ask our service to set an alarm for that date, this activity talks to the client that talks to the service
    scheduleClient.setAlarmForNotification(calendar,"Checkin");
    scheduleClient.setAlarmForNotification(calendar1, "Checkout");

我希望通知会在正确的时间,正确的上下文中完全弹出。

0 个答案:

没有答案