0

I'm trying to build an app that call a method in activity when a certain data is received from server, I use socket for my conection to server and when a certain message recived it calls Task3 which I want to call a method in activity, this is a part of my code for data ,which is a shared code to use in ios and android what should I do ?

public string Task3(ClientClass NCC)
{
    string servermessage = ByteToString(NCC.NetMessageResived); 
}
Adrian C.
  • 1,647
  • 1
  • 23
  • 27
Reza Najafi
  • 141
  • 1
  • 11

2 Answers2

0

One way to achieve this is by using BroadcastReceivers. In your class that received the message from the server you can raise an intent like

String serverResponse = ...;
String action = "MESSAGE_RECEIVED_FROM_SERVER";
Intent intent = new Intent();
intent.setAction(action);
intent.putExtra("msg", serverResponse );

context.sendBroadcast(intent);

And now in your activity you need to create an Broadcast Receiver that will register for the MESSAGE_RECEIVED_FROM_SERVERintent. And when receiving the intent you can call the desired method from the activity.

You can use an example from tutorialspoint or vogella.

Hope it helps.

Adrian C.
  • 1,647
  • 1
  • 23
  • 27
0

Depending on your scenario u can adopt given below methods

  • Try using an interface

https://stackoverflow.com/a/16443645/4247543

  • if the above method is not helping then try using EventBus

please follow below link to know more of Event Bus

https://github.com/greenrobot/EventBus

http://gunhansancar.com/ease-communication-between-activities-fragments-services/

  • BroadcastReceiver as mentioned could also help for enabling communication.

https://stackoverflow.com/a/10084754/4247543

  • You can use observers

https://stackoverflow.com/a/30964385/4247543

Hopes it helps.

Community
  • 1
  • 1
Rissmon Suresh
  • 13,173
  • 5
  • 29
  • 38