是否可以动态更新清单中定义的 BroadcastReceiver 的 IntentFilter?
Is it possible to dynamically update the IntentFilter of a BroadcastReceiver defined in the manifest?
我正在尝试为清单中定义的 BroadcastReceiver 动态更新 intentfilter。意图在我的应用程序之外的 API 中声明。
最终目标是在应用程序关闭时让这些操作在后台执行,但如果我在没有取消注册的情况下离开应用程序,我动态创建的 BroadcastReceiver 会抛出泄漏错误消息。
我这样做的方式正确吗?是否可以动态更新清单中声明的内容?
如何在 API 中声明意图:
public static final String RECEIVED_MESSAGE = Constants.NAMESPACE_PREFIX +
".action." + "RECEIVED_MESSAGE";
public static final String SEND_MESSAGE = Constants.NAMESPACE_PREFIX +
".action." + "SEND_MESSAGE";
public static final String MESSAGE_SENT = Constants.NAMESPACE_PREFIX +
".action." + "MESSAGE_SENT";
public static final String MESSAGE_NOT_SENT = Constants.NAMESPACE_PREFIX +
".action." + "MESSAGE_NOT_SENT";
public static final String[] ALL = new String[] { RECEIVED_MESSAGE, SEND_MESSAGE, MESSAGE_SENT,
MESSAGE_NOT_SENT };
我是如何创建 Intentfilter 的:
private IntentFilter createIntentFilter()
{
IntentFilter filter = new IntentFilter();
for(String action : MessageActions.ALL)
filter.addAction(action);
return filter;
}
干杯!
无法在 运行 时更改这些 Intent 过滤器。您唯一可以真正改变的是启用了哪些组件。充其量你可能需要在后台 运行 一个 Service
来注册并保持动态 BroadcastReceiver
.
鉴于您发布的代码,我不明白为什么不能在清单中静态定义的 BroadcastReceiver
中使用这些代码。除非 Constants.NAMESPACE_PREFIX
不是编译时常量表达式,否则所有这些字符串都是编译时常量,您应该能够在 AndroidManifest 中使用它们(尽管在那里进行了硬编码,因为您无法引用任何内容 Java ).
我正在尝试为清单中定义的 BroadcastReceiver 动态更新 intentfilter。意图在我的应用程序之外的 API 中声明。
最终目标是在应用程序关闭时让这些操作在后台执行,但如果我在没有取消注册的情况下离开应用程序,我动态创建的 BroadcastReceiver 会抛出泄漏错误消息。
我这样做的方式正确吗?是否可以动态更新清单中声明的内容?
如何在 API 中声明意图:
public static final String RECEIVED_MESSAGE = Constants.NAMESPACE_PREFIX +
".action." + "RECEIVED_MESSAGE";
public static final String SEND_MESSAGE = Constants.NAMESPACE_PREFIX +
".action." + "SEND_MESSAGE";
public static final String MESSAGE_SENT = Constants.NAMESPACE_PREFIX +
".action." + "MESSAGE_SENT";
public static final String MESSAGE_NOT_SENT = Constants.NAMESPACE_PREFIX +
".action." + "MESSAGE_NOT_SENT";
public static final String[] ALL = new String[] { RECEIVED_MESSAGE, SEND_MESSAGE, MESSAGE_SENT,
MESSAGE_NOT_SENT };
我是如何创建 Intentfilter 的:
private IntentFilter createIntentFilter()
{
IntentFilter filter = new IntentFilter();
for(String action : MessageActions.ALL)
filter.addAction(action);
return filter;
}
干杯!
无法在 运行 时更改这些 Intent 过滤器。您唯一可以真正改变的是启用了哪些组件。充其量你可能需要在后台 运行 一个 Service
来注册并保持动态 BroadcastReceiver
.
鉴于您发布的代码,我不明白为什么不能在清单中静态定义的 BroadcastReceiver
中使用这些代码。除非 Constants.NAMESPACE_PREFIX
不是编译时常量表达式,否则所有这些字符串都是编译时常量,您应该能够在 AndroidManifest 中使用它们(尽管在那里进行了硬编码,因为您无法引用任何内容 Java ).