Gtalk登陆

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");

			   

		   }

	    }	 

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值