Android 如何在悬挂式帐篷中发送有序广播?
我想发送一个挂在帐篷里的有序广播。但我只找到了Android 如何在悬挂式帐篷中发送有序广播?,android,android-pendingintent,Android,Android Pendingintent,我想发送一个挂在帐篷里的有序广播。但我只找到了PendingIntent.getBroadcast(this,0,intent,0),我认为它只能发送常规广播。那么,我能做些什么呢?我是从以下方面得到的: 如果onFinished参数不为null,则执行有序广播 因此,您可能希望尝试使用onFinished参数集调用 但是,我遇到了一个问题,我必须从通知发送OrderedBroadcast。 我通过创建一个BroadcastReceiver来实现它,它只是将意图作为OrderedBroadcas
PendingIntent.getBroadcast(this,0,intent,0)
,我认为它只能发送常规广播。那么,我能做些什么呢?我是从以下方面得到的:
如果onFinished参数不为null,则执行有序广播
因此,您可能希望尝试使用onFinished参数集调用
但是,我遇到了一个问题,我必须从通知发送OrderedBroadcast。
我通过创建一个BroadcastReceiver来实现它,它只是将意图作为OrderedBroadcast转发。我真的不知道这是否是一个好的解决方案
因此,我首先创建了一个意图,其中包含了要转发的操作的名称,作为额外的:
// 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.
就我而言,我将挂起的内容传递给了一个通知:
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);
然后,我在清单中定义了以下接收者:
<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>
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);
}
}