14.01.2013 Views

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 9: Peer-to-Peer Communication<br />

282<br />

// When the service connects, get the default GTalk Session<br />

public void onServiceConnected(ComponentName className, IBinder service) {<br />

gtalkService = IGTalkService.Stub.asInterface(service);<br />

}<br />

// If the service disconnects<br />

public void onServiceDisconnected(ComponentName className) {<br />

gtalkService = null;<br />

}<br />

};<br />

A bound GTalk Service represents a connection between your application and the GTalk Service APIs.<br />

Before you can use the Service to use Android’s Instant Messaging functionality, you need to initiate a<br />

new GTalkConnection, as shown in the following section.<br />

Making a GTalk Connection and Starting an IM Session<br />

A GTalk Connection represents a conduit between the device and a GTalk server. An IM Session is the<br />

message pathway used to handle all the instant message traffi c; all the instant messages for a given session<br />

fl ow through this pipe.<br />

You can create several different connections and multiple IM Sessions connecting to different GTalk<br />

servers or IM providers.<br />

Under normal circumstances, a device needs a single GTalk Connection supporting a single IM<br />

Session that uses the device owner’s username. You can access the default connection and session<br />

using getDefaultConnection and getDefaultSession on the GTalk Service and default connection,<br />

respectively, as shown in the snippet below:<br />

IGTalkConnection gTalkConnection = gtalkService.getDefaultConnection();<br />

IImSession imSession = gTalkConnection.getDefaultImSession();<br />

IM Sessions are used to send text and data messages, set user presence, manage the IM contact roster,<br />

and manage group chats.<br />

The IM Session is your primary interface for handling instant messaging in Android applications. As<br />

a result, the following code snippet shows a more typical implementation of the ServiceConnection<br />

used to bind the GTalk Service to an application. It ensures that an IM Session object is always valid.<br />

private IGTalkConnection gTalkConnection = null;<br />

private IImSession imSession = null;<br />

private ServiceConnection gTalkServiceConnection = new ServiceConnection() {<br />

// When the service connects, get the default GTalk session.<br />

public void onServiceConnected(ComponentName className, IBinder service) {<br />

IGTalkService gtalkService = IGTalkService.Stub.asInterface(service);<br />

try {<br />

gTalkConnection = gtalkService.getDefaultConnection();<br />

imSession = gTalkConnection.getDefaultImSession();<br />

} catch (RemoteException e) { }

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!