AnsweredAssumed Answered

How to update TextView in function processMessage?

Question asked by 奇真亦假 on Nov 22, 2015
Latest reply on Dec 1, 2015 by Yesid Lazaro

I am a beginner in Android, And i am trying to use smack4.1.4 in my app.

 

Code:

public class MainActivity extends AppCompatActivity {

 

   @Override
   protected void onCreate(Bundle savedInstanceState) {

   super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_main);

   if (android.os.Build.VERSION.SDK_INT > 9) {

  StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

  StrictMode.setThreadPolicy(policy);

  }

  TextView text=(TextView)this.findViewById(R.id.textview);

  MyChatMessageListener messageListener=new MyChatMessageListener(text);

  XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder();

  String DOMAIN="xxxxx";

  builder.setServiceName(DOMAIN);

  builder.setHost(DOMAIN);

  builder.setPort(5222);

  builder.setCompressionEnabled(false);

  builder.setDebuggerEnabled(false);

  builder.setSendPresence(true);

  builder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);

  XMPPTCPConnection connection = new XMPPTCPConnection(builder.build());

   try {

  connection.connect();

  connection.login("testone","123456");

  } catch (Exception e) {

  e.printStackTrace();

  }

  text.setText("asdfasdf");

  ChatManager chatmanager = ChatManager.getInstanceFor(connection);

  chatmanager.addChatListener(messageListener);

  Chat Chat = chatmanager.createChat("testtwo@xxxxx");

 

   try {

  Chat.sendMessage("Howdy!");

  }

   catch (Exception e) {

  e.printStackTrace();

  }

  }

 

class MyChatMessageListener implements ChatManagerListener

{

  TextView textView;

 

  MyChatMessageListener(TextView _textView)

  {

   textView=_textView;

  }

 

   public void chatCreated(Chat chat, boolean createdLocally) {

  chat.addMessageListener(new ChatMessageListener() {

   @Override
   public void processMessage(Chat chat, Message message) {

   Log.v("Message:",message.getBody());

   textView.setText(message.getBody());

  }

  });

  }

}

 

The "Log.v" works but not the "textView.setText"

 

So who can tell me how to update the textView in this callback function? I will very appreciate.

Outcomes