简体   繁体   English

如何使用待处理的Intent Android发送Order广播接收器

[英]How to send Order broadcast receiver using pending intent android

Hi i have function like when user hit on notification i have to check my application is in foreground if it so, just close the notification. 嗨,我有功能,如当用户点击通知时,我必须检查我的应用程序是否在前台,如果关闭的话,只需关闭通知即可。 Otherwise need to open up the application. 否则需要打开应用程序。

I have use concept of ordered broadcast to achieve but i am stuck to call ordered broadcast receiver from pending intent. 我使用有序广播的概念来实现,但是我被迫从待定意图中调用有序广播接收器。

To send an ordered broadcast using a PendingIntent , use one of the send() methods, for example this one , that takes a PendingIntent.OnFinished argument. 要使用PendingIntent发送有序广播,请使用send()方法之一 ,例如this方法, 方法采用PendingIntent.OnFinished参数。 This capability is not explicitly documented and only the description of the parametersto PendingIntent.OnFinished gives some hint that ordered broadcasts are supported. 此功能未明确记录,仅对PendingIntent.OnFinished的参数描述提供了一些提示,表明支持有序广播。

Here is example for sending an ordered broadcast: 这是发送有序广播的示例:

Intent i = new Intent("com.my.package.TEST_ACTION");

PendingIntent.OnFinished listener = new PendingIntent.OnFinished() {
    @Override
    public void onSendFinished(PendingIntent pendingIntent, Intent intent,
                               int resultCode, String resultData, Bundle resultExtras) {
        Log.i("TEST", String.format("onSendFinished(): result=%d action=%s",
                            resultCode, intent.getAction()));
    }
};

PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);

int initResult = -1;

try {
    pi.send(initResult, listener, null);
} catch (PendingIntent.CanceledException e) {
    e.printStackTrace();
}

I confirmed that this produces an ordered broadcast by defining a number of receivers with this general form, registered in the manifest with different priorities: 我确认通过定义许多具有此一般格式的接收器(在清单中注册的优先级不同)来产生有序广播:

public class ReceiverA extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("AAAA", String.format("result=%d ordered=%b", getResultCode(), isOrderedBroadcast()));
        setResultCode(1111);
    }
}

The logcat output confirmed that the receivers were invoked in the expected order, that isOrderedBroadcast() is true for each, and the result code set by setResultCode() is passed to the next receiver, and finally to the PendingIntent.OnFinished callback. logcat输出确认已按预期顺序调用了接收方,即每个接收方的isOrderedBroadcast()为true,并将setResultCode()设置的结果代码传递给下一个接收方,最后传递给PendingIntent.OnFinished回调。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM