Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send an ordered broadcast in a PendingIntent?

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?

like image 711
user1455175 Avatar asked Jun 14 '12 02:06

user1455175


People also ask

What is ordered broadcast?

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 ...

How do I send data from BroadcastReceiver to activity?

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.

What is the use of broadcast receiver in android?

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.

What is a pending intent?

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.


1 Answers

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);
    }
}
like image 150
sroes Avatar answered Sep 20 '22 12:09

sroes