`
xsuo
  • 浏览: 118964 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

Gtalk基本功能完成

阅读更多

Gtalk基本功能完成

上传源代码,供大家参考!
ContactsActivity(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;

    private static final int contactsActivity = 0;

    public static IGTalkService xmppservice;

    

    @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) {

			  xmppservice = IGTalkService.Stub.asInterface(service);

			  try {

				//enter account,password  

				mGtalkSession = xmppservice.createGTalkSession(login_User.getText().toString(),login_Pwd.getText().toString());

				mGtalkSession.requestRoster();

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

                				break;

                			case 5:

                				mGtalkSession.setPresence(new Presence(Im.PresenceColumns.AVAILABLE, "Am here now!"));

                				createContact();

                				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) {

			

				e.printStackTrace();

			}

		}

        

         //service disconnected

		public void onServiceDisconnected(ComponentName componentname) {

			xmppservice = null;

		}

    	

    };

	     //tow buttons bindservic/eunbindservice 

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

			   xmppservice = null;

			   logMessage("ServiceDisconnected");

			   

		   }

	    }

   

        private void createContact() {

		    Intent contact = new Intent(this,ContactsActivity.class);

		    startSubActivity(contact, contactsActivity);

		}

		

		protected void onActivityResult(int requestCode, int resultCode, String data, Bundle extras) {

	    	super.onActivityResult(requestCode, resultCode, data, extras);

	        

	        switch(requestCode) {

	        case contactsActivity:

	            break;

	        }

	    }



}
MessageSender (发消息)
import com.google.android.gtalkservice.IChatSession;

import com.google.android.gtalkservice.IGTalkService;

import com.google.android.gtalkservice.IGTalkSession;

import android.app.Activity;

import android.database.Cursor;

import android.os.Bundle;

import android.os.DeadObjectException;

import android.provider.Im;

import android.widget.Button;

import android.widget.EditText;

import android.widget.ListAdapter;

import android.widget.ListView;

import android.widget.SimpleCursorAdapter;

import android.widget.TextView;

import android.widget.Toast;

import android.view.View;



public class MessageSender extends Activity implements View.OnClickListener {

    private EditText senderEdit = null;

    private ListView mesgList = null;

    private Button sendButn = null;

    private TextView recevieView = null;

    IGTalkSession mXmppSession = null;

    IGTalkService xmppService;

    private String user = null;

	@Override

	protected void onCreate(Bundle icicle) {

		super.onCreate(icicle);

		setContentView(R.layout.message);

		

		recevieView = (TextView)this.findViewById(R.id.recipient);

		senderEdit = (EditText)this.findViewById(R.id.sendText);

		mesgList = (ListView)this.findViewById(R.id.listMessages);

		sendButn = (Button)this.findViewById(R.id.send);

		sendButn.setOnClickListener(this);

		

		Bundle extras = getIntent().getExtras();

		if (extras != null) {

			user = extras.getString(ContactsActivity.KEY_Name);

	//		logMessage("***********user************"+user);

            if(user != null){

            	recevieView.setText(user);

            	

            	Cursor cursor = managedQuery(Im.Messages.CONTENT_URI, null,

                        null, null, null);

                ListAdapter adapter = new SimpleCursorAdapter(this,

                        android.R.layout.simple_list_item_1,

                        cursor, 

                        new String[]{Im.MessagesColumns.BODY},

                        new int[]{android.R.id.text1});



                this.mesgList.setAdapter(adapter);

            }

            

            

            

		}

		

		

		

	}

     

	private void logMessage(CharSequence msg) {

        Toast.makeText(MessageSender.this, msg,

                Toast.LENGTH_SHORT).show();

    }

	

	public void onClick(View view) {

		if(view == sendButn ){

			try{

				xmppService = GtalkActivity.xmppservice; 

				if(xmppService == null){

					

					logMessage("xmppService is null");

				}

				try {

					mXmppSession = xmppService.getDefaultSession();

					

				} catch (DeadObjectException e) {

					// TODO Auto-generated catch block

					e.printStackTrace();

				}

				if(mXmppSession == null){

					logMessage("mXmppSession is null");

					return;

				}

				

			 IChatSession ichatsession = mXmppSession.createChatSession(user);

			 if(ichatsession == null) 

           	{

           		logMessage("Fail to create chat session. Return.");

           		return;

           	}

			ichatsession.sendTextMessage(senderEdit.getText().toString()); 

			} catch (DeadObjectException e) {

				e.printStackTrace();

			}

				

		}

		

	}

   

}
AndroidGtalk.rar (59.1 KB)
AndroidGtalk.rar (59.1 KB) 下载次数: 0
2008-6-17 22:37
分享到:
评论
1 楼 常思己过 2011-09-05  
楼主,下载链接出现问题,可否修改下

相关推荐

Global site tag (gtag.js) - Google Analytics