I want to send an ordered broadcast in a PendingIntent. But I've only found PendingIntent.getBroadcast(this, 0, intent, 0)
, which I think can only send a regular broadcast. So, what can I do?
In ordered mode, broadcasts are sent to each receiver in order (controlled by the android:priority attribute for the intent-filter element in the manifest file that is related to your receiver) and one receiver is able to abort the broadcast so that receivers with a lower priority would not receive it (thus never ...
Step 1. Open your project where you want to implement this. Step 2. Open your BroadcastReceiver class from where you pass data to activity inside your onReceive() you need to start intent and pass data inside intent and start sendBroadcast() as shown bellow.
A broadcast receiver (receiver) is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android runtime once this event happens.
A PendingIntent itself is simply a reference to a token maintained by the system describing the original data used to retrieve it. This means that, even if its owning application's process is killed, the PendingIntent itself will remain usable from other processes that have been given it.
I got this from http://justanapplication.wordpress.com/tag/pendingintent-getbroadcast:
If the onFinished argument is not null then an ordered broadcast is performed.
So you might want to try calling PendingIntent.send with the onFinished argument set.
However, I ran into the problem that I had to send an OrderedBroadcast from a Notification. I got it working by creating a BroadcastReceiver which just forwards the Intent as an OrderedBroadcast. I really don't know whether this is a good solution.
So I started out by creating an Intent which holds the name of the action to forward to as an extra:
// the name of the action of our OrderedBroadcast forwarder
Intent intent = new Intent("com.youapp.FORWARD_AS_ORDERED_BROADCAST");
// the name of the action to send the OrderedBroadcast to
intent.putExtra(OrderedBroadcastForwarder.ACTION_NAME, "com.youapp.SOME_ACTION");
intent.putExtra("some_extra", "123");
// etc.
In my case I passed the PendingIntent to a Notification:
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle("Notification title")
.setContentText("Notification content")
.setSmallIcon(R.drawable.notification_icon)
.setContentIntent(pendingIntent)
.build();
NotificationManager notificationManager = (NotificationManager)context
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify((int)System.nanoTime(), notification);
Then I defined the following receivers in my Manifest:
<receiver
android:name="com.youapp.OrderedBroadcastForwarder"
android:exported="false">
<intent-filter>
<action android:name="com.youapp.FORWARD_AS_ORDERED_BROADCAST" />
</intent-filter>
</receiver>
<receiver
android:name="com.youapp.PushNotificationClickReceiver"
android:exported="false">
<intent-filter android:priority="1">
<action android:name="com.youapp.SOME_ACTION" />
</intent-filter>
</receiver>
Then the OrderedBroadcastForwarder looks as follows:
public class OrderedBroadcastForwarder extends BroadcastReceiver
{
public static final String ACTION_NAME = "action";
@Override
public void onReceive(Context context, Intent intent)
{
Intent forwardIntent = new Intent(intent.getStringExtra(ACTION_NAME));
forwardIntent.putExtras(intent);
forwardIntent.removeExtra(ACTION_NAME);
context.sendOrderedBroadcast(forwardIntent, null);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With