Android XMPP - getting roster
I’m playing around with android and xmpp, part of the android API that I find very interesting. Unfortunately the API seems a little incomplete still, so everyone playing with this is being forced to use some hacks. The Android SDK comes with ApiDemos/apps/XmppDataSender and XmppDataReceiver. After getting connected (you can only connect to google talk servers), the demo applications are meant to send data between 2 android instances. Since they are DATA clients, you cannot use the Demos to chat. You have to have 2 instances of the android emulator running. 1 instance of the emulator slows down my poor machine noticeably, I haven’t had the courage to try running two. With my computer being kind of flakey anyway, this doesn’t seem like a good idea. So… challenge to self: create google talk chat client for Android. I’ll go by pieces, then hope to tie them together at the end. I guess the main pieces are 1) send 2) receive 3) present roster 4)move between activities (roster screen and msg screen) then 5) make it nicer.
1) Turns out to be trivial. In the sample code XmppDataMessageSender the onClick event,comment out the sendDataMessage and put in a sendTextMessage, like below.
try {
//mXmppSession.sendDataMessage(username, getIntentToSend());
mXmppSession.sendTextMessage(username, 0, msgtosend);
} catch (DeadObjectException ex) {}
voila, now you can send messages from Android to your other gtalk account.
2) Turns out to be harder. How do you register an Intent to receive text messages? We’ll come back to that in a bit
3) The roster. There actually is documentation that mentions how this can be done requestRosterAndSendInitialPresence .. so.. here’s the code for getting a roster and presenting it.
try {
mXmppSession.requestRosterAndSendInitialPresence();
} catch (DeadObjectException ex) {
Log.e(LOG_TAG, “caught ” + ex);
mXmppSession = null;
bindXmppService();
}
try {
if(mXmppSession.hasReceivedRoster()){
Cursor cursor = managedQuery(Im.Presence.CONTENT_URI, null,null, null, null);
Log.d(LOG_TAG, “got ” + cursor.count() + ” contacts”);
ListAdapter adapter = new SimpleCursorAdapter(this,
R.layout.show_roster_items,
cursor,
new String[]{Im.Contacts.USERNAME,Im.Contacts.PRESENCE_STATUS},
new int[]{R.id.contactname,R.id.contactstatus});
mInMessage.setAdapter(adapter);
}
} catch (DeadObjectException ex) {
Log.e(LOG_TAG, “caught ” + ex);
}
Thats the code. I’ll attach the rest of the project later, mainly the .xml files for displaying the above.
Note that this only gets users that are currently online. It does not retrieve your whole buddy list. I don’t know why. I also have not figured out where im/contacts lives, since it is not in the Im database (I guess that is worth another post).
Right now I’m going to try to use the roster for selecting a user, pass it to the xmppdatasender.
Não há comentários. »
Ainda não há comentários.
Alimentação RSS de comentários a este artigo. | TrackBack URI
Deixe um comentário
You must be logged in to post a comment.
