GTalk服务绑定

为了使用GTalk服务,你需要使用bindService将其绑定到应用程序组件上。

 

bindService方法接受2个输入参数:1Intent(指明了要绑定的组件)和1ServiceConnection实现。下面的框架代码演示了如何绑定GTalk服务:

 

IGTalkService gtalkService;

 

private void bindGTalk() {

Intent i = new Intent();

i.setComponent(GTalkServiceConstants.GTALK_SERVICE_COMPONENT);

bindService(i, gTalkConnection, 0);

}

 

private ServiceConnection gTalkConnection = new ServiceConnection() {

 

// When the service connects, get the default GTalk Session

public void onServiceConnected(ComponentName className, IBinder service)

{

gtalkService = IGTalkService.Stub.asInterface(service);

}

 

// If the service disconnects

public void onServiceDisconnected(ComponentName className)

{

gtalkService = null;

}

};

 

绑定的GTalk服务表示你的应用程序与GTalk服务API之间建立了连接。在你能使用GTalk服务来使用Android的即时消息功能之前,你还需要初始化一个新的GTalkConnection,如接下来的章节所示。
posted on 2009-09-28 22:22  xirihanlin  阅读(1215)  评论(0编辑  收藏  举报