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

时间:2015-07-05 21:15:22

标签: android alarmmanager android-pendingintent

我一直试图解决这个问题几个小时,似乎无法找出问题所在。我正在创建一个警报,每天在指定的小时和分钟重复。如果我在通知发出之前的任何时候强制关闭应用程序,那么就没有问题。然而,在通知消失后我强行关闭然后再次发射。每次重新打开并强制关闭应用程序时,这都会反复发生。

我分别在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;
}

1 个答案:

答案 0 :(得分:0)

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

说明:AlarmManager创建了ReminderService,没有任何障碍。问题是该服务在启动通知后仍然存在。然后在强制关闭后,它会持续存在并触发另一个通知。