Login     Sign Up
Cyril Sermon (@admin)
9 months ago
52 Views

You can get a list of participants in a Chat Session using the getParticipants method. You can also

send text or data messages to each chat member as you would in a normal chat, as well as invite new

members using inviteContact. The leave method lets you exit a chat room and end the session.

As with normal chats, you can listen to chat room messages by implementing and registering an

IChatListener. As well as listening for chat messages, you can react to people joining or leaving

the room.

The following skeleton code shows the implementation of a Chat Listener highlighting the group chat

event handlers:

IChatListener groupChatListener = new IChatListener.Stub() {
 // Fired when a one-to-one chat becomes a group chat.
 public void convertedToGroupChat(String oldJid, 
 String groupChatRoom,
 long groupId) throws RemoteException {
 // TODO Notify user that the conversation is now a group chat.
 }
 // Fired when a new person joins a chat room.
 public void participantJoined(String groupChatRoom, String nickname)
 throws RemoteException {

 // TODO Notify user that a new participant has joined the conversation.
 }
 // Fired when a participant leaves a chat room.
 public void participantLeft(String groupChatRoom, String nickname) 
 throws RemoteException {
 // TODO Notify user a chat participant left.
 }
 // Fired when the group chat is closed
 public void chatClosed(String groupChatRoom) throws RemoteException {
 // TODO Close the chat.
 }
 public void chatRead(String arg0) throws RemoteException { }
 public void newMessageReceived(String from, String body) { }
};

Sending and Receiving Data Messages

The GTalk Service includes functionality to transmit data messages between applications running on

different devices. These data messages are handled separately from normal text chat messages and are

invisible to users.

The functionality described in this section was removed prior to the version

1.0 release of the Android SDK. This is largely because of the security implications associated with the ability to remotely execute code on a target device. It is

expected that this API will be exposed for developer access in future releases of

Android, although it may differ from the implementation described here.

GTalk data messages are a mechanism that lets you broadcast Intents over the air (OTA) to remote user

devices. On the target device, the GTalk Service extracts the Intent from the received message and rebroadcasts it locally, where it’s handled by the Intent resolution mechanism in the same way as locally

broadcast Intents. The process is illustrated in Figure 9-1.

Source Device

My

Application

IMSession

Intent

Target Device

GTalkService

Bro

The result is an interface for broadcasting Intents on remote devices using instant messenger contacts. The broadcast Intent will be received by any Broadcast Receiver registered for the action represented by the Intent.

By extending the reach of your applications beyond the scope of the device on which they’re running, you take on additional responsibilities to ensure that your applications are well behaved, and to take all possible precautions to ensure that your applications aren’t open to exploitation by those looking to use this mechanism maliciously.

Data messages are an excellent way to support multi-user applications on distributed mobile devices, thanks to the low latency and rapid response times provided by the instant messaging architecture.