Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6333911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:33:27+00:00 2026-05-24T18:33:27+00:00

I’m implementing XMPP client for an Android application. For getting the chat messages that

  • 0

I’m implementing XMPP client for an Android application. For getting the chat messages that are sent to me, I’m using the PacketListener from Smack. With the XMPP part of the application, everything works fine. I can send and receive messages. But I’m having problems displaying the received messages.

For displaying messages, my application uses an ArrayAdapter that binds them to a ListView. The adapter itself works fine, since it displays the messages I send without any problems. But not so with the received messages. They are just displayed if some interaction with the UI happens. Apparently, this is a threading issue.

If I’m not mistaken by what the Javadoc and the Debugger tell me, the PacketListener.processPacket() method runs in an own thread, and the update of the ListView is only executed if the Handler has a next thing to do and therefore processes it. My question is now, how can I tell the Handler to process it immediately? How does the communication between this worker thread and the main thread work here? Since I didn’t make a Runnable myself, I don’t know how to handle this.

And here’s the code:

public class Chat extends Activity {
    private ArrayList<String> mMessages;
    private ArrayAdapter<String> mAdapter;
    private ListView mMessageListView;
    private EditText mInput;
    private String mRecipient;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chat);

        Bundle extras = getIntent().getExtras();
        mRecipient = extras.getString("jabberid");
        mMessages = new ArrayList<String>();
        mMessageListView = (ListView) findViewById(R.id.chatMessageList);
        mInput = (EditText) findViewById(R.id.chatInput);
        mAdapter = new ArrayAdapter<String>(this, R.layout.channelentry, mMessages);
        mAdapter.notifyDataSetChanged();
        mMessageListView.setAdapter(mAdapter);

        // Getting messages
        PacketFilter packetFilter = new MessageTypeFilter(Message.Type.chat);
        // XMPPConnection already connected and authenticated
        XmppManager.connection.addPacketListener(new PacketListener() {

            // Here is where it doesn't display the received message
            @Override
            public void processPacket(Packet packet) {
                Message message = (Message) packet;
                displayMessage(message);
            }
        }, packetFilter);

        // Sending messages
        Button send = (Button) findViewById(R.id.chatSend);
        send.setOnClickListener(new View.OnClickListener() {

            // Here everything works just fine
            @Override
            public void onClick(View v) {
                Message message = new Message(mRecipient, Message.Type.chat);
                message.setBody(mInput.getText().toString());
                XmppManager.connection.sendPacket(message);
                displayMessage(message);
            }
        });
    }

    private void displayMessage(Message message) {
        String sender = message.getFrom();
        String chat = sender + " > " + message.getBody();
        mAdapter.add(chat);
        mAdapter.notifyDataSetChanged();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T18:33:28+00:00Added an answer on May 24, 2026 at 6:33 pm

    If you create a Handler within your UI thread, you can call post() on it with a Runnable argument that calls your displayMessage() method. Alternatively, you can call runOnUiThread(), which is part of the Activity class, again, passing a Runnable that calls displayMessage().

    I’ve also noticed that you call sendPacket() from your onClick() handler. You should make sure that you don’t block the UI thread. Maybe sendPacket() will actually spawn a new thread to do the actual send, but it’s something that you should check.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am currently running into a problem where an element is coming back from
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.