Triggering a toast from a listener

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

Answers


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.


Need Your Help

Displaying percentage values in gridview

c# asp.net sql-server-2008 gridview

I am trying to display values in percentage format in Gridview. Right now I am able to get percentage values along with its symbol but what i need is the Range. I am storing % values through a Drop...

XPath select wsdl prefix namespaces

java xml soap xpath wsdl

I am using XPath 1.0 in java and would like to select/check wsdl document namespaces, but unfortunately did not managed to do it.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.