Я устанавливаю AlarmManager на несколько тревог, когда выбираю дату из календаря для AlarmManager, а AlarmManager должен срабатывать 2 раза. Во время 24 часа это 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