Android AlarmManager不会取消相同的Intents

时间:2013-05-26 12:42:37

标签: android serialization alarmmanager

在过去的两天里,我一直试图让AlarmManager取消之前设置的警报,因为某些原因我无法让它工作。

我已经搜索了问题,最常见的解决方案是跟踪旧意图,因为只有那些,它们才可以取消。

为了解决这个问题,我决定创建自己的Intent类,它是Serializable。

然后我将Intent类保存到“Intent.ser”文件中,并调用那些取消我的警报。

SerUitvalIntent.java

public class SerUitvalIntent extends Intent implements Serializable
{

private String klas;
private int delay;
private Boolean uitvalBuzz;
private Boolean algemeenBuzz;

public SerUitvalIntent(Context context, Class<?> class1)
{
    super(context, class1);
}

public void setBundle(Bundle b)
{
    klas = b.getString(someClass.KLAS);
    delay = b.getInt(someClass.DELAY);
    uitvalBuzz = b.getBoolean(someClass.UITVALBUZZ);
    algemeenBuzz = b.getBoolean(someClass.ALGEMEENBUZZ);
}

public Bundle getBundle()
{
    Bundle b = new Bundle();
    b.putBoolean(someClass.UITVALBUZZ, uitvalBuzz);
    b.putBoolean(someClass.ALGEMEENBUZZ, algemeenBuzz);
    b.putString(someClass.KLAS, klas);
    b.putInt(someClass.DELAY, delay);
    return b;
}

/**
 * 
 */
private static final long serialVersionUID = 8289838097824161902L;
}

someClass.java

public static void schedule(Context context, long delay, Bundle data, Boolean startup)
{
    DataHandler dHandle = new DataHandler(context, false);
    final AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    SerUitvalIntent oldIntent = dHandle.getAlarm();

    if(oldIntent != null)
    {
        oldIntent.putExtra(BUNDLE, oldIntent.getBundle());
        oldIntent.putExtra(FROMUI, false);
        Bundle b = oldIntent.getBundle();
        Log.i("AlarmManagerUitval", "Cancelled " + b.getString(KLAS) + "." + b.getInt(DELAY) + "." + b.getBoolean(UITVALBUZZ) + " alarm");
        final PendingIntent oldPending = PendingIntent.getService(context, 0, oldIntent, 0);
        //Cancel latest alarm
        alarm.cancel(oldPending);
    }


    final SerUitvalIntent intent = new SerUitvalIntent(context, UitvalService.class);
    intent.setBundle(data);
    dHandle.setAlarm(intent);

    intent.putExtra(BUNDLE, data);
    intent.putExtra(FROMUI, false);

    final PendingIntent pending = PendingIntent.getService(context, 0, intent, 0);

    Log.i("AlarmManagerUitval", "Started " + data.getString(KLAS) + "." + data.getInt(DELAY) + "." + data.getBoolean(UITVALBUZZ) + " alarm");
    long time;

    if(startup)
    {
        time = SystemClock.elapsedRealtime();
    }
    else
    {
        time = SystemClock.elapsedRealtime()+delay;
    }
    alarm.setRepeating(AlarmManager.ELAPSED_REALTIME, time,delay, pending);
}

我的Logcat在设置和取消警报时显示相同的数据(Log.i()显示相同的数据),但不知道它不会取消。

Logcat数据

05-26 14:21:00.108: I/AlarmManagerUitval(26041): Started v4.60000.true alarm
05-26 14:21:00.178: I/UitvalService(26041): updating: v4
(...)
05-26 14:21:30.227: I/AlarmManagerUitval(26041): Cancelled v4.60000.true alarm
05-26 14:21:30.237: I/AlarmManagerUitval(26041): Started h4.60000.true alarm
05-26 14:21:30.307: I/UitvalService(26041): updating: h4
(...)
05-26 14:22:30.526: I/UitvalService(26104): updating: v4
05-26 14:23:30.244: I/UitvalService(26104): updating: v4

1 个答案:

答案 0 :(得分:0)

我放弃了:P

我在警报中保存了我想要发送的数据,并且每当我想要取消之前的警报时重新创建一个通用的Intent。

然后我回忆起警报调用的Intent上保存的数据。

如果有人以某种方式找到了解决这个问题的正确答案,我仍然有兴趣知道。 :)

Wampie