gpt4 book ai didi

Android 自定义 Intent 过滤器没有收到广播?

转载 作者:搜寻专家 更新时间:2023-11-01 09:06:23 28 4
gpt4 key购买 nike

我有一个带有指定单个自定义类别的 Intent 过滤器的广播接收器:

    <receiver
android:name=".sys.sub.SubServiceManager"
android:enabled="true"
android:exported="false" >
<intent-filter>
<category android:name="com.example.SUB_SERVICE_STATE" />
</intent-filter>
</receiver>

然后是使用带有自定义操作的 Intent 类别发出广播的代码。我的问题是从未收到广播。

    Intent speed = new Intent();
speed.addCategory(inst.getString(R.string.subServiceCat));
speed.setAction("com.example." + SubServiceManager.START_COMMAND + Example.NAME);
inst.sendBroadcast(speed);

R.string.subServiceCat 包含与 intent-filter 中定义的相同的字符串。

对于自定义类别 intent 过滤器,我还需要做些什么吗?

最佳答案

Documentation说:

To pass this test(Action test ), the action specified in the Intent object must match one of the actions listed in the filter. If the object(intent) or the filter does not specify an action, the results are as follows:

  • If the filter fails to list any actions, there is nothing for an intent to match, so all intents fail the test. No intents can get through the filter.
  • On the other hand, an Intent object that doesn't specify an action automatically passes the test — as long as the filter contains at least one action.

你看到你的过滤器没有指定一个 Action ,所以你的 Intent 无法通过过滤器。要解决这个问题,你可以为你的接收者的过滤器指定一个 Action ,或者不为你的 Intent 指定任何 Action 。

关于Android 自定义 Intent 过滤器没有收到广播?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11605987/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com