Question

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();
  }
}
Was it helpful?

Solution

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.

OTHER TIPS

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());

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top