-1

I am creating an android application using smack and openfire in android.I have used a listener for user presences changes.. Below is my code..

toolbar_status=(TextView)findViewById(R.id.toolbar_status) ;
    roster.addRosterListener(new RosterListener() {
                @Override
                public void entriesAdded(Collection<String> addresses) {

                }

                @Override
                public void entriesUpdated(Collection<String> addresses) {

                }

                @Override
                public void entriesDeleted(Collection<String> addresses) {

                }

                @Override
                public void presenceChanged(Presence presence) {
                    Log.wtf("CRA","Presence changes---------------");
                    Log.wtf("CRA","new presence is : "+presence.getFrom()+" "+presence.isAvailable());
                    toolbar_status.setText(setStatus(presence.isAvailable()));
                }
            });

As you can see i am updating the textview named as toolbar_status with current value... The problem presenceChanged function is called and even Logs are also displayed but the current value is not updated at run time.The value is set to text view when i reopens the activity.. That is,inside presenceChanged function i cannot interact with UI at run time.. I am a beginner in android.Please help me where am i wrong how can i sort it out..

Mohit Gaur
  • 355
  • 1
  • 3
  • 22

1 Answers1

1

presenceChanged() may be getting called from a background thread (i'm not sure, not familiar with Smack).

If so, you cannot touch the UI from there.

You could do the following:

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        toolbar_status.setText(setStatus(presence.isAvailable()));
    }
});

Which will post this Runnable to the message queue of the main thread (from where the UI can be updated).

earthw0rmjim
  • 19,027
  • 9
  • 49
  • 63