How to create different pendingintent so filterEquals() return false?

本小妞迷上赌 提交于 2019-11-29 01:16:43

You could try using the requestCode parameter when creating your pending intent:

PendingIntent pi =PendingIntent.getBroadcast(mContext, yourUniqueDatabaseId,i,PendingIntent.FLAG_ONE_SHOT);

This should create an Intent that is unique for matching purposes..

public boolean filterEquals(Intent other) compare the action, data, type, package, component, and categories, but do not compare the extra, so the difference between extras cannot distinguish Intents. Try to set different data to these Intents, the data could be insignificant Uris.

Try to read the source code to get more infomation.

Trying putting a dummy action in the intent. Eg.

myIntent.setAction(""+System.currentTimeMillis());

This will cause filterEquals to return false.

I needed a similar solution for sending sms notifications from multiple numbers, in order to allow the notification to startup the sms thread viewer with different numbers.

Put the task id and a timestamp into the action of the intent. That way, you associate the pending intent with the task id and the intents will still be different in terms of filtering.

intent.setAction("ACTION_" + taskId + "_" + currentTimestamp);

In my case I didn't have a good way to generate a unique key for my pending intents so what I did was add custom data to the generated intent.

Once you start filling the data attribute, you have to remember to add a data parameter to the intent-filter in the android manifest. If you don't do this it will not trigger your custom receiver.

Examples:

intent

    Intent intent = new Intent(reminderFire);
intent.addCategory(reminderCategory);
Uri.Builder builder = new Uri.Builder();
builder.scheme(context.getString(R.string.scheme));
builder.authority(context.getString(R.string.intent_filter_reminder_host));
builder.appendQueryParameter("oid", Reminder.getOid());
intent.setData(builder.build());

AndroidManifest entry

<receiver 
    android:name=".client.MyReminder"
    android:enabled="true"
>
    <intent-filter>
        <action android:name="com.example.client.reminder.fire"/>
        <action android:name="com.example.client.reminder.category"/>
        <data
            android:host="@string/intent_filter_reminder_host"
            android:scheme="@string/scheme" />
    </intent-filter>
</receiver>

By doing this you can also test uniqueness in the intent before it gets added as a pending intent, which is useful if you're unit testing.

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