import com.google.android.gtalkservice.IGTalkService;
import com.google.android.gtalkservice.IGTalkSession;
import com.google.android.gtalkservice.Presence;
import android.view.View;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.DeadObjectException;
import android.os.IBinder;
import android.provider.Im;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class GtalkActivity extends Activity implements View.OnClickListener {
private EditText login_User = null;
private EditText login_Pwd = null;
private IGTalkSession mGtalkSession = null;
private Button Btn_Login = null;
private Button Btn_Exit = null;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
//EditText: account,password
login_User = (EditText)this.findViewById(R.id.login_et_user);
login_Pwd = (EditText)this.findViewById(R.id.login_et_pwd);
//Button: login,exit
Btn_Login = (Button)this.findViewById(R.id.btn_login);
Btn_Exit = (Button)this.findViewById(R.id.btn_exit);
Btn_Login.setOnClickListener(this);
Btn_Exit.setOnClickListener(this);
}
//display message on screen
private void logMessage(CharSequence msg) {
Toast.makeText(GtalkActivity.this,msg,
Toast.LENGTH_SHORT).show();
}
//service connection
private ServiceConnection mConnection = new ServiceConnection(){
public void onServiceConnected(ComponentName className, IBinder service) {
IGTalkService xmppservice = IGTalkService.Stub.asInterface(service);
try {
//enter account,password
mGtalkSession = xmppservice.createGTalkSession(login_User.getText().toString(),login_Pwd.getText().toString());
if(mGtalkSession == null){
logMessage(getText(R.string.xmpp_session_not_found));
return;
}
if(mGtalkSession != null){
boolean b = false;
try {
b = mGtalkSession.isConnected();
} catch (DeadObjectException ex) {
logMessage("***Check Current State meets Error! Exception->" + ex);
}
if(b){
//connection state
switch(mGtalkSession.getConnectionState()){
case 4: // login successfully
logMessage("successfully logged in to the XMPP server!");
mGtalkSession.setPresence(new Presence(Im.PresenceColumns.AVAILABLE, "Am here now!"));
break;
case 5:
logMessage("the client requested roster from the server.");
break;
case 0: // network problem
logMessage("***ConnectionState.IDLE -> not connected!");
break;
case 2: // connecting....
logMessage("connecting to the server.");
break;
case 3:
logMessage("connected to the server, but not authenticated yet.");
break;
case 1:
logMessage("in a pending state for automatic reconnection.");
break;
default:
logMessage("Unknown ConnectionState!");
break;
}
}
}
} catch (DeadObjectException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//service disconnected
public void onServiceDisconnected(ComponentName componentname) {
mGtalkSession = null;
}
};
//tow buttons bindservice/unbindservice
public void onClick(View view) {
if(view == Btn_Login){
bindService((new Intent()).setComponent(com.google.android.gtalkservice.GTalkServiceConstants.GTALK_SERVICE_COMPONENT),
mConnection,Context.BIND_AUTO_CREATE);
}
if(view == Btn_Exit){
unbindService(mConnection);
logMessage("ServiceDisconnected");
}
}
}
Gtalk登陆
最新推荐文章于 2021-03-02 09:56:46 发布