【问题标题】:Trying to have a Broadcast Receiver with No filter试图有一个没有过滤器的广播接收器
【发布时间】:2012-08-10 20:25:55
【问题描述】:
IntentFilter intentFilter = new IntentFilter("test");
registerReceiver(mReceiver, intentFilter);

我不希望有像 registerReceiver(mReceiver, null) 这样的过滤器,但我的应用程序因此而崩溃。我可以将new IntentFiler() 设为空文件管理器吗?

【问题讨论】:

    标签: android broadcastreceiver


    【解决方案1】:

    因为当通过IntentFilter 的条件不匹配时,BroadcastReceiver 返回null,所以 API 无法完成您希望完成的任务(我假设发送任何和所有广播到mReceiver)。

    您当然可以指定一个空的IntentFilter,但这将毫无用处,因为注册接收器不会导致它捕获任何广播(除非它们直接针对接收器,正如 MisterSquonk 在 cmets 中提到的那样) .否则,您必须确切知道要使用 BroadcastReceiver 捕获哪些广播,然后在 IntentFilter 中指定条件。

    【讨论】:

    • "...注册接收器不会导致它捕获任何广播。" - 你确定吗?可以在没有<intent-filter> 元素的清单中使用<receiver> 元素注册BroadcastReceiver。在这种情况下,您可以通过显式指定其类来向该接收者发送Intent,就像您直接针对Activity 一样。例如,Intent i = new Intent(this, MyBroadcastReceiver.class);
    • @MisterSquonk,真的,感谢您提出这个问题。我在考虑非定向广播。我会更新我的帖子。
    • 是的,我想我们不应该那么懒惰,还试图在没有任何字符串集的情况下发送 Intent
    猜你喜欢
    • 2012-02-26
    • 1970-01-01
    • 2016-11-06
    • 1970-01-01
    • 1970-01-01
    • 2021-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多