GTalk服务绑定
为了使用GTalk服务,你需要使用bindService将其绑定到应用程序组件上。
bindService方法接受2个输入参数:1个Intent(指明了要绑定的组件)和1个ServiceConnection实现。下面的框架代码演示了如何绑定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;
}
};