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)
分享到:
相关推荐
GTalk 绿色单文件版
Google Talk(Gtalk)是Google公司开发的即时通讯工具,Gtalk除了具备一般IM功能外,还加上了Viop功能,Gtalk界面清晰大方,可直接链接Gmail,轻松接受邮件。欢迎下载最新版google talk。
gtalk样式自定义方法
gtalk.apk-android
Gtalk_1.3.apk
Gtalk是google出的一款即时聊天工具。 这个源码是采用c#开发的客户端。 可以用来学习用作Gtalk的再开发,制作高级功能。
公司要做个获取MSN 和Gtalk好友列表的ASP.net程序实现朋友邀请功能,网上找了很久都没找到任何有用的东西。 无奈自己摸索写出一个例子,希望对以后需要的人有所帮助
android4.0以上,给没有装google服务包的人。
有些单位只能上gtalk,但不能与qq.MSN通讯用这个软件就可以实现
难得资源,gtalk是google聊天工具,挂着几乎没有流量,发消息与接消息占多少用多少流浪,飞信比QQ省流量,精简飞信比飞信省流量,gtalk比精简飞信还省流量。挂着用吧。热爱网络,珍惜收藏
google聊天工具,GTalk英文版,可以进行文件传输,聊天信息进行了加密
android 仿gtalk 源代码 挺有意思的 可以参考借鉴下
黑莓上的gtalk+gmail邮件提醒工具:testa。 相应速度非常快,第一时间告知您有新的gmail邮件了。
手机统一通信平台Gtalk接口研究,王丽娜,,目前网络上有各种通信方式,有即时聊天通信软件-QQ、Gtalk、MSN,还有一些SNS交友社区的一些通信方式,依据手机统一通信平台的建设目�
完美解决android电子市场不能下载,Gtalk、Gmail不能正常使用……弄法见内部操作指南
架设在GAE上的GTalk翻译机器人 **目前已经存在的功能: 1. (0.1版开始支持) 可以直接识别中文和英文,并自动进行互译 1. (0.1版开始支持) 可以翻译一段文章,而不单单只是一个单词** **以后可能会有的功能: 1. (0.2...
GTALK机器人程序,可按照事件自动通知好友,也可接受用户消息做出相应反应
gtalk接口二次开发告警程序,直接用。
解释Gtalk的实现,基于xmpp扩展协议。
界面和Gtalk非常类似的第三方程序,登录模式里有http方式,但选择后似乎不能使用CMWAP方式连接,而用CMNET(TCP连接方式)没有出现问题。 更多系统支持,访问 http://www.talkonaut.com/ 您的电话号码处,之需要输入...