六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 20|回复: 0

Gtalk登陆

[复制链接]

升级  9%

67

主题

67

主题

67

主题

举人

Rank: 3Rank: 3

积分
227
 楼主| 发表于 2013-1-15 02:42:00 | 显示全部楼层 |阅读模式
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 blocke.printStackTrace();}}         //service disconnectedpublic 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");      }    } }
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表