2

I'm using an external package defining a JunctionActor. The idea is that a JunctionActor can send JSON messages to a remote server via a method called sendMessage and receive messages via a listener calling onMessageReceived. In my implementation, the device receives every message it sends, thus calling onMessageReceived. Yet, in my code, I've included a ChatClient object in my activity class, which has to call the show_message method. show_message triggers a Toast. When I'm calling show_message from onJoin, there is no problem at all, but when it's called from onMessageReceived, nothing shows up, whereas my debugger tells me that the app indeed receives a message and that onMessageReceived is triggered. In the mean time, the call of show_message in the onJoin method actually works. And I can't see any difference between both of them. Do you have a solution ?

Thanks a lot

public class HelloWorldJunctionActivity extends Activity {
     onCreate(...){...} [...]
private class ChatClient extends JunctionActor {
      public ChatClient() {
        super("client");
      }
      public void onActivityJoin() {
          show_message("Connected");
      }
      @Override
      public void onMessageReceived(MessageHeader header, JSONObject msg) { 
          try { 
            show_message(msg.getString("text"));
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            show_message(e.getMessage());
        }
      }
}
void show_message(String message) {
      Toast
        .makeText(HelloWorldJunctionActivity.this, message, Toast.LENGTH_SHORT)
        .show();
  }
}
Flavian Hautbois
  • 2,940
  • 6
  • 28
  • 45

2 Answers2

2

I'm not sure how JunctionActor works, but is it possible that the onMessageReceived callback is invoked from a thread which is not the UI thread? If that's the case you might have to use a Handler, as explained in this post.

Community
  • 1
  • 1
gianpi
  • 3,110
  • 1
  • 17
  • 13
  • JunctionActor is provided by an external jar. As it is asynchronous, I guess it effectively uses an a thread which is not the UI thread. This is really helpful, I will test it ASAP. Thanks – Flavian Hautbois Nov 30 '11 at 20:58
0

The reason it is not toasting is because the context object HelloWorldJunctionActivity.this is non existent. Try sending the context object as well

void show_message(String message, Context con) { Toast .makeText(con, message, Toast.LENGTH_SHORT) .show(); } }

show_message("Connected", getApplicationContext());

Pavan K
  • 4,085
  • 8
  • 41
  • 72
  • 1
    My bad, I was not precise enough. Actually my activity is not called myActivity but HelloWorldJunctionActivity. Plus, the call of show_message in the onJoin method triggers a toast, contrary to the one if onMessageReceived. – Flavian Hautbois Nov 30 '11 at 20:29
  • I did not understnad if you got it or not. Well if that answers your question then mark it answered :) – Pavan K Nov 30 '11 at 20:40
  • Your assumption is that show_message doesn't work because I didn't pass the right context. But I did, I can see toast messages, so the problem is not there. – Flavian Hautbois Nov 30 '11 at 20:50