当前位置: 首页 > article >正文

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)
http://www.lryc.cn/news/2418487.html

相关文章:

  • Win7系统提示找不到audiosrv.dll文件的解决办法
  • NOD32离线升级更新包使用方法
  • C# ASP.NET校园外卖网站管理系统源码 前台+后台
  • iebook超级精灵2008 专业版破解
  • robots协议相关知识(摘转自360百科)
  • 安卓系统一键root
  • Vue.js教程
  • 【单片机】2.8 AT89S52单片机的最小应用系统
  • 模型蒸馏探索(Bert)
  • 3D模型动画素材来源
  • 光棍节程序员闯关秀第1关(总共10关)
  • shsh备份工具_A12A13 iOS13备份shsh2简易工具/附详细教程
  • Temple Run的终点
  • 文件下载时直接对流进行zip加密压缩
  • 百度文心一言api 调用ERNIE-3.5-8K,Python技术开发文档
  • 三维装箱模型
  • 从技术支持看PLC制造商的差距
  • 给弟弟的国产山寨机安装软件
  • 常用的几种brush
  • 中标麒麟7.0+linux内核版本,中标麒麟7.0下载
  • 常见面试题之 1000的阶乘后面有多少个0
  • lsass.exe和smss.exe病毒专杀工具——即磁碟机病毒专杀工具(转载)
  • VS2005 SP1补丁下载与安装
  • VC++路径
  • 安卓表格布局android:collapseColumns,android:shrinkColumns和stretchColumn
  • Linux mail 命令(smtp.163.com)
  • oracle number()类型,ORACLE NUMBER类型详解
  • JavaVM和JNIEnv
  • [教程]Smarty 入门
  • 强烈推荐10本程序员必读的书