Setting multiple Intents for AlarmManager

℡╲_俬逩灬. 提交于 2019-12-10 12:02:03

问题


I use AlarmManager to schedule multiple taks, i.e. Task 1 at 10:56, Task 2 at 11:24 and so on. Here's the code:

  intent = new Intent(ACTION_RECORDER_START);
  intent.putExtra(EXTRA_COMMAND_ID, command.id);
  pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
  alarmManager.set(AlarmManager.RTC_WAKEUP, command.start, pendingIntent);

Here's the strange thing: If I set one alarm, it does job well. If I set two, only the last alarm is fired. So, I guess the problem is that when I set second, third...alarm, previous is overriden.

From developer docs:

If there is already an alarm for this Intent scheduled (with the equality of two intents being defined by filterEquals(Intent)), then it will be removed and replaced by this one.

I went to the sources, here's the implementation of that method:

public boolean filterEquals(Intent other) {
        if (other == null) {
            return false;
        }
        if (mAction != other.mAction) {
            if (mAction != null) {
                if (!mAction.equals(other.mAction)) {
                    return false;
                }
            } else {
                if (!other.mAction.equals(mAction)) {
                    return false;
                }
            }
        }
        if (mData != other.mData) {
            if (mData != null) {
                if (!mData.equals(other.mData)) {
                    return false;
                }
            } else {
                if (!other.mData.equals(mData)) {
                    return false;
                }
            }
        }
        if (mType != other.mType) {
            if (mType != null) {
                if (!mType.equals(other.mType)) {
                    return false;
                }
            } else {
                if (!other.mType.equals(mType)) {
                    return false;
                }
            }
        }
        if (mPackage != other.mPackage) {
            if (mPackage != null) {
                if (!mPackage.equals(other.mPackage)) {
                    return false;
                }
            } else {
                if (!other.mPackage.equals(mPackage)) {
                    return false;
                }
            }
        }
        if (mComponent != other.mComponent) {
            if (mComponent != null) {
                if (!mComponent.equals(other.mComponent)) {
                    return false;
                }
            } else {
                if (!other.mComponent.equals(mComponent)) {
                    return false;
                }
            }
        }
        if (mCategories != other.mCategories) {
            if (mCategories != null) {
                if (!mCategories.equals(other.mCategories)) {
                    return false;
                }
            } else {
                if (!other.mCategories.equals(mCategories)) {
                    return false;
                }
            }
        }

        return true;
    }

So, as far as I see, no mention of extras. In fact, I was relying on this:

intent.putExtra(EXTRA_COMMAND_ID, command.id);

but standart implementation doesn't compare exras. So, when I schedule multiple intents, they are compared equal and get overriden!

The actual question:

How do I override filterEquals(Intent), so that I can distinguish Intents based on Extras?

Here's my implementation:

 static class AlarmIntent extends Intent{

        public AlarmIntent(String action){
            super(action);
        }

        @Override
        public boolean filterEquals(Intent other){
            if(super.filterEquals(other)){
                long id  = getExtras().getLong(AudioRecorder.EXTRA_COMMAND_ID, -1);
                long otherId = other.getExtras().getLong(AudioRecorder.EXTRA_COMMAND_ID, -1);
                if(id == otherId){
                    return true;
                }
            }
            return false;
        }
    }

But it seems to me it doesn't work. I think that overriden filterEquals is not called.


回答1:


Ended up with this:

pendingIntent = PendingIntent.getService(context, UNIQUE_ID, intent, PendingIntent.FLAG_ONE_SHOT);

Providing second argument to getService did the job.



来源:https://stackoverflow.com/questions/11630460/setting-multiple-intents-for-alarmmanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!