Gtalk基本功能完成

Gtalk基本功能完成

上传源代码,供大家参考!
ContactsActivity(Gtalk好友列表)
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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值