How to send the ordered broadcast to PendingIntent?

I want to send an ordered broadcast to PendingIntent. But I found PendingIntent.getBroadcast(this, 0, intent, 0) , which, I think, can only send a normal broadcast. So what can I do?

+6
source share
1 answer

I got this from http://justanapplication.wordpress.com/tag/pendingintent-getbroadcast :

If the onFinished argument is not null, an ordered translation is performed.

So you can try calling PendingIntent.send with a set of onFinished arguments.

However, I ran into a problem that I had to send OrderedBroadcast from Notification. I got his job by creating a BroadcastReceiver that simply redirects the intent as an order. I really don't know if this is a good solution.

So, I started by creating an Intent that contains the name of the action to forward as an optional:

 // 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 the 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 OrderedBroadcastForwarder looks like this:

 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); } } 
+4
source

Source: https://habr.com/ru/post/918051/


All Articles