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();
}
}
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.
I modified your code as follows.Hope it will work now.
public class Chat extends Activity {
private ArrayList<String> mMessages;
private ArrayAdapter<String> mAdapter;
private ListView mMessageListView;
private EditText mInput;
private String mRecipient;
String chat;
@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);
String sender = message.getFrom();
chat = sender + " > " + message.getBody();
Message msg = handler.obtainMessage();
msg.arg1 = 1;
handler.sendMessage(msg);
}
}, 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();
}
private final Handler handler = new Handler() {
public void handleMessage(Message msg) {
if(msg.arg1 == 1){
mAdapter.add(chat);
mAdapter.notifyDataSetChanged();
}
}
}
}
精彩评论