Android AlarmManager 在强制关闭应用程序时触发

Android AlarmManager fires when force closing the app

我已经尝试解决这个问题几个小时了,但似乎无法弄清楚问题出在哪里。我正在创建一个每天在指定的小时和分钟重复的警报。如果我在通知触发之前的任何时候强制关闭应用程序,那么就没有问题。但是,在通知关闭并且我强行关闭后它再次触发。每次我重新打开并强行关闭应用程序时,这种情况都会反复发生。

我在 onPause 和 onCreate 期间分别将内部存储中的提醒作为 JSON 个对象保存和加载。

正在创建待定意图

    public void addReminder(Reminder reminder){
    if(!reminders.contains(reminder)) {
        reminders.add(reminder);

        //Set up alarm
        Intent myIntent = new Intent(context, ReminderService.class);
        //hh*60+mm as the ID makes sure the pending intent is unique
        PendingIntent pendingIntent = PendingIntent.getService(context, (reminder.getHours()*60)+reminder.getMinutes(), myIntent, 0);
        AlarmManager alarmManager = (AlarmManager)context.getSystemService(context.ALARM_SERVICE);
        reminder.setPendingIntent(pendingIntent, alarmManager);

        //start alarm
        reminder.startReminder();
    }
    Collections.sort(reminders);

Reminder.java

public class Reminder implements Comparable<Reminder> {
private Date date;
private final String JSON_DATE = "date";
private final String TAG = Reminder.class.getSimpleName();
private PendingIntent pendingIntent;
private AlarmManager alarmManager;

public Reminder(Date date){
    this.date = date;
}

//get values from JSON object
public Reminder(JSONObject json) throws JSONException {
    date = new Date(json.getLong(JSON_DATE));
}

//turn values into JSON
public JSONObject toJSON() throws JSONException {
    JSONObject json = new JSONObject();
    json.put(JSON_DATE, date.getTime());

    return json;
}

@Override
public int compareTo(Reminder r){
    return (new Integer(getHours()*60+getMinutes())).compareTo(new Integer(r.getHours() * 60 + r.getMinutes()));
}

@Override
public boolean equals(Object o){
    Reminder r = (Reminder)o;
    if(r.getDateTime().getTime() == getDateTime().getTime())
        return true;
    return false;
}

public void setPendingIntent(PendingIntent pendingIntent, AlarmManager alarmManager){
    this.pendingIntent = pendingIntent;
    this.alarmManager = alarmManager;
}

public void startReminder(){
    Log.d(TAG, "Creating Reminder for:" + getHours() + ":" + getMinutes());

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, getHours());
    calendar.set(Calendar.MINUTE, getMinutes());
    calendar.set(Calendar.SECOND, 00);
    calendar.set(Calendar.MILLISECOND, 0);

    Calendar nowCalendar = Calendar.getInstance();

    //check if equal to current time then set to tomorrow if so
    if(nowCalendar.getTimeInMillis() >= calendar.getTimeInMillis())
        calendar.add(Calendar.DATE, 1);

    this.date = calendar.getTime();

    Log.d(TAG, "New Reminder:" + (String) android.text.format.DateFormat.format("hh:mm:ss", getDateTime()));

    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}

public void close(){
    Log.d(TAG, "Destroying Reminder");
    alarmManager.cancel(pendingIntent);
}

public String getString(){
    Calendar cal = Calendar.getInstance();
    cal.setTime(getDateTime());
    if (cal.get(Calendar.AM_PM) == Calendar.PM) {
        String time = (String) android.text.format.DateFormat.format("hh:mm", getDateTime());
        time += " PM";
        return time;
    }
    String time = (String) android.text.format.DateFormat.format("hh:mm", getDateTime());
    time += " AM";
    return time;
}

public int getHours(){
    Calendar calendar = GregorianCalendar.getInstance(); // creates a new calendar instance
    calendar.setTime(date);   // assigns calendar to given date
    return calendar.get(Calendar.HOUR_OF_DAY);
}

public int getMinutes(){
    Calendar calendar = GregorianCalendar.getInstance(); // creates a new calendar instance
    calendar.setTime(date);   // assigns calendar to given date
    return calendar.get(Calendar.MINUTE);
}

public Date getDateTime(){
    return date;
}

我通过在 ReminderService 的 onCreate 中创建通知后添加 stopSelf() 来修复此问题。

说明:AlarmManager 顺利创建了 ReminderService。问题是该服务在发出通知后挂起。然后在强制关闭后,它会持续存在并触发另一个通知。