gpt4 book ai didi

java - 无法接收来自服务的广播

转载 作者:行者123 更新时间:2023-11-30 02:19:55 25 4
gpt4 key购买 nike

我遇到的问题是关于从 android 库项目中的服务接收广播。

为什么收不到广播?它是否与单独的 android 库项目中的所有服务/接口(interface)逻辑有关?

这是服务的广播

    broadcastReceivedMessage("THE TOPIC", "THE MESSAGE");

这是服务中的方法

    private void broadcastReceivedMessage(final String topic, final String message) {
/* handler.post(new Runnable() {
@Override
public void run() {*/
Intent broadcastIntent = new Intent(CALLBACK_ACTION);
broadcastIntent.setAction(ON_MESSAGE_RECEIVED);
broadcastIntent.putExtra(TOPIC_STR, topic);
broadcastIntent.putExtra(MESSAGE_STR, message);
sendBroadcast(broadcastIntent);
/* }
});*/

}

在实现许多接口(interface)的类中,这是我注册以监听特定 Intent 过滤器的地方。

  @Override
public void connect(String address, int port) {
IntentFilter filter = new IntentFilter(MQTTService2.CALLBACK_ACTION);
LocalBroadcastManager.getInstance(getContext()).registerReceiver(serviceActionReceiver, filter);
Bundle args = new Bundle();
args.putString(MQTTService2.BROKER_ADDR_STR, address);
args.putInt(MQTTService2.BROKER_PORT_INT, port);
startService(MQTTService2.ACTION_CONNECT, args);
}

在一个 Activity 中,我希望从接口(interface)接收回调并获取我的广播

  MQTTEventHandler mHandler = new MQTTEventHandler() {

@Override
public void onStatusChanged(CommunicatorStatus status, String message) {
// TODO Auto-generated method stub
Log.e("onStatusChanged", "----------------Status Changed------------");
Log.e("STATUS: ", status.toString());
Log.e("MESSAGE: ", message);
Log.e("onStatusChanged", "--------------------------------------------");

}

@Override
public void onMessageReceived(String topic, String message) {
// TODO Auto-generated method stub
Log.e("onMessageReceived", "----------------Message Received------------");
Log.e("TOPIC: ", topic);
Log.e("MESSAGE: ", message);
Log.e("onMessageReceived", "--------------------------------------------");

}

@Override
public void onException(String message) {
// TODO Auto-generated method stub

}
};

mComm.addEventHandler(mHandler);

广播接收者

class ServiceActionReceiver extends BroadcastReceiver {



@Override
public void onReceive(Context context, Intent intent) {

String action = intent.getAction();
if (action.equals(MQTTService2.ON_EXCEPTION)) {
Bundle extras = intent.getExtras();
if (extras != null && extras.containsKey(MQTTService2.MESSAGE_STR))
fireOnException(extras.getString(MQTTService2.MESSAGE_STR));
} else if (action.equals(MQTTService2.ON_MESSAGE_RECEIVED)) {
Bundle extras = intent.getExtras();
if (extras != null && extras.containsKey(MQTTService2.MESSAGE_STR)
&& extras.containsKey(MQTTService2.TOPIC_STR))
fireOnMessageReceived(extras.getString(MQTTService2.TOPIC_STR),
extras.getString(MQTTService2.MESSAGE_STR));

} else if (action.equals(MQTTService2.ON_STATUS_CHANGED)) {
Bundle extras = intent.getExtras();
if (extras.containsKey(MQTTService2.STATUS_CODE)) {
MQTTService2.MQTTConnectionStatus status =
MQTTConnectionStatus.parseCode(extras.getString(MQTTService2.STATUS_CODE));
switch (status) {
case CONNECTED:
fireOnStatusChanged(CommunicatorStatus.DISCONNECTED,
extras.getString(MQTTService2.MESSAGE_STR, ""));
break;
case CONNECTING:
break;
case INITIAL:
break;
case NOTCONNECTED_DATADISABLED:
case NOTCONNECTED_UNKNOWNREASON:
case NOTCONNECTED_USERDISCONNECT:
case NOTCONNECTED_WAITINGFORINTERNET:
fireOnStatusChanged(CommunicatorStatus.DISCONNECTED,
extras.getString(MQTTService2.MESSAGE_STR, ""));
break;
default:
break;

}
}

}
}
}

根据特定的 Intent 过滤器调用的方法(onMessageReceived)

      private void fireOnMessageReceived(String topic, String message) {
for (MQTTEventHandler eventHandler : handlers) {
eventHandler.onMessageReceived(topic, message);
}
/* for (Iterator<MQTTEventHandler> it = handlers.iterator(); it.hasNext();) {
it.next().onMessageReceived(topic, message);
}*/
}

最佳答案

似乎以下内容导致从您的服务广播的 Intent 与 BroadcastReceiver 上设置的 IntentFilter 不匹配。

Intent broadcastIntent = new Intent(CALLBACK_ACTION);
broadcastIntent.setAction(ON_MESSAGE_RECEIVED);

IntentFilter 是为 CALLBACK_ACTION 设置的,Intent 正在用相同的操作实例化,但是上面的第二行将 Intent 的操作重置为 ON_MESSAGE_RECEIVED,导致它不再匹配 BroadcastReceiver 上的过滤器。

一种可能的解决方案是向 IntentFilter 添加多个操作,并在 Receiver 的 onReceive() 方法中检查传递的操作以确定如何处理广播。

例如:

IntentFilter filter = new IntentFilter(MQTTService2.ACTION_MESSAGE_RECEIVED);
filter.addAction(MQTTService2.ACTION_OTHER);

然后,在onReceive()方法中:

@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();

if(action.equals(ACTION_MESSAGE_RECEIVED)) {
...
}
else if(action.equals(ACTION_OTHER)) {
...
}
...
}

然后您将使用所需的操作实例化广播 Intent,然后删除 setAction() 调用:

Intent broadcastIntent = new Intent(MQTTService2.ACTION_MESSAGE_RECEIVED);
...

关于java - 无法接收来自服务的广播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28745209/

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