How to Create a XMPP Chat Client in Android with Openfire?
I am a newbie to Android. I wanted to create a XMPP chat client along with push notifications. I have installed openfire and the server is ready for testing on my local machine.
At this point what I am doing 1. Creating a XMPP connection in ASYNC task 2. onPost creating a chat manager
protected void onPostExecute(final Boolean success) {
if (success) {
if (connection != null) {
chatManager = connection.getChatManager();
Chat newChat = chatManager.createChat("shobhit",
new MessageListener() {
public void processMessage(Chat chat, Message message) {
//Do something
}
});
}
}
}
But I am totally confused if I am doing this is correctly. I read somewhere on stackoverflow that ASYNC should be used only for small duration activities and my connection can get timed out here.
I also found these as probable solutions: Runnable, Handlers Create a Service Broadcast receivers
I am not sure which method should I opt. Can you guys suggest the optimum method and pointers to the example of that would be great?
Thanks Chetan
As you said, ASYNC should be used only for small duration activities. Assuming that you use some version of Asmack in Android, the connection has to be in a separate thread from the activity (as a networking process) so you have to use a new thread/runnable for the connection to XMPP server. Asmack use its own threads after that, but you shouldn't worry about it. With this template, you can create simple application which may be killed anytime by android. If you want something to resist long-time, you should use a service for the XMPP part
Use AsynTask to create connection, In android all network related task should be different thread than UI thread so call it from doInBackground() method. If you want to keep the connection for long time put it in service.
链接地址: http://www.djcxy.com/p/64986.html