删除待处理的意图

时间:2013-08-25 07:39:07

标签: android android-intent broadcastreceiver android-pendingintent

我正在尝试从正在使用待处理意图进行广播的警报管理器中删除警报,并且使用广播接收器来捕获并执行该进程,调用deleteReminder函数,但意图仍在触发在时机成熟时。 如果代码中有问题请告诉我。

public class ReminderManager {

private Context mContext;
private AlarmManager mAlarmManager;
private Intent i;

public ReminderManager(Context context) {
    mContext = context;
    mAlarmManager = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
}
public void setReminder(Long taskId, Calendar when) {

    System.out.println("setReminder Called");

    i = new Intent(mContext, Receiver.class);
    i.putExtra(Database.KEY_ROWID, (long) taskId);

    Toast.makeText(mContext, "setReminder" + Fragment.uniqueId,
            Toast.LENGTH_SHORT).show();
    PendingIntent pi = PendingIntent.getBroadcast(mContext,
            Fragment.uniqueId, i, PendingIntent.FLAG_ONE_SHOT);

    mAlarmManager.set(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), pi);
}

public void deleteReminder(int uniqueID) {

    System.out.println("deleteReminder Called");
    i = new Intent(mContext, Receiver.class);
    i.putExtra(Database.KEY_ROWID, (long) uniqueID);

    Toast.makeText(mContext, "deleteReminder" + uniqueID,
            Toast.LENGTH_SHORT).show();

    PendingIntent.getBroadcast(mContext, uniqueID, i,
            PendingIntent.FLAG_UPDATE_CURRENT).cancel();
            mAlarmManager.cancel(PendingIntent.getBroadcast(mContext, uniqueID, i,
            PendingIntent.FLAG_UPDATE_CURRENT));
}
}

2 个答案:

答案 0 :(得分:0)

此代码适用于我。

public void deleteReminder(int uniqueID) {

    System.out.println("deleteReminder Called");
    i = new Intent(mContext, Receiver.class);
    i.putExtra(Database.KEY_ROWID, (long) uniqueID);

    Toast.makeText(mContext, "deleteReminder" + uniqueID,
            Toast.LENGTH_SHORT).show();

    PendingIntent pi = PendingIntent.getBroadcast(mContext,
            Fragment.uniqueId, i, PendingIntent.FLAG_CANCEL_CURRENT);

    mAlarmManager.cancel(pi);

}

答案 1 :(得分:0)

我在setReminder函数中添加了一个布尔值来检查我是否要删除,并在警报管理器中添加或删除警报的if语句,它减小了代码的大小并且工作正常。

public class ReminderManager {

private Context mContext;
private AlarmManager mAlarmManager;
private Intent i;

// Constructor to set the context and set the alarmManager
public ReminderManager(Context context) {
    mContext = context;
    mAlarmManager = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
}

// used to create a pending intent using task id and Calendar object
public void setReminder(Long taskId, Calendar when, boolean delete) {

    System.out.println("setReminder Called");

    i = new Intent(mContext, OnAlarmReceiver.class);
    i.putExtra(NotesDatabase.KEY_ROWID, (long) taskId);

    Toast.makeText(mContext, "setReminder" + AddFragment.uniqueId,
            Toast.LENGTH_SHORT).show();
    // broadcast !! // change the
    PendingIntent pi = PendingIntent.getBroadcast(mContext,
            AddFragment.uniqueId, i, PendingIntent.FLAG_ONE_SHOT);

    if(delete == false){
    mAlarmManager.set(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), pi);
    }else{
        pi.cancel();
        mAlarmManager.cancel(pi);
    }
}