我正在尝试构建一个应用程序,当从服务器收到某些数据时调用autoreconf -i
中的方法,我使用activity
来连接到服务器以及何时收到某个消息它调用socket
我想在活动中调用方法,这是我的数据代码的一部分,这是在Task3
和ios
中使用的共享代码我应该怎么做?
android
答案 0 :(得分:0)
实现此目标的一种方法是使用BroadcastReceivers
。在从您的服务器收到消息的类中,您可以提出类似
String serverResponse = ...;
String action = "MESSAGE_RECEIVED_FROM_SERVER";
Intent intent = new Intent();
intent.setAction(action);
intent.putExtra("msg", serverResponse );
context.sendBroadcast(intent);
现在,在您的活动中,您需要创建一个Broadcast Receiver
来注册MESSAGE_RECEIVED_FROM_SERVER
意图。收到意图后,您可以从活动中调用所需的方法。
您可以使用tutorialspoint或vogella中的示例。
希望它有所帮助。
答案 1 :(得分:0)
根据您的情况,您可以采用以下方法
https://stackoverflow.com/a/16443645/4247543
请按照以下链接了解更多活动巴士
https://github.com/greenrobot/EventBus
http://gunhansancar.com/ease-communication-between-activities-fragments-services/
https://stackoverflow.com/a/10084754/4247543
https://stackoverflow.com/a/30964385/4247543
希望有所帮助。