zoukankan      html  css  js  c++  java
  • Android IM Note

    http://www.igniterealtime.org/downloads/index.jsp

    在官网下载openfire服务器,Spark工具。
    下载asmack jar包。
    点击bin目录下的openfired.exe开启服务器。设定登录服务器:http://localhost:9090
     
    暂时只实现文字聊天
    XmppTool.java
     
    import org.jivesoftware.smack.ConnectionConfiguration;
    import org.jivesoftware.smack.XMPPConnection;
    import org.jivesoftware.smack.XMPPException;
    public class XmppTool {
    private static XMPPConnection con = null;
    /**
    * get and open XMPPConnection
    * @return
    */
    public static XMPPConnection getConnection(){
    if(con == null){
    openConnection();
    }
    return con;
    }
     
    /**
    * close XMPPConnection
    */
    public static void closeConnection(){
    con.disconnect();
    con = null;
    }
     
    private static void openConnection(){
    ConnectionConfiguration cc = new ConnectionConfiguration("192.168.27.65", 5222);  //here is your server ip address and port
    con = new XMPPConnection(cc);
    try {
    con.connect();
    } catch (XMPPException e) {
    e.printStackTrace();
    }
    }
    }
     
     
    IM_oneActivity.java
     
    import org.jivesoftware.smack.Chat;
    import org.jivesoftware.smack.ChatManager;
    import org.jivesoftware.smack.MessageListener;
    import org.jivesoftware.smack.XMPPException;
    import org.jivesoftware.smack.packet.Message;
    import org.jivesoftware.smack.packet.Presence;
     
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.text.method.ScrollingMovementMethod;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;
     
    public class _IM_oneActivity extends Activity implements OnClickListener {
     
    private TextView isLogined;
    private TextView chat;
    private Button send;
    private EditText message;
     
    private ChatManager chatMana;
    private Chat newChat;
    private Message receiveMsg;
    private Message sendMsg;
     
    private Handler handler = new Handler() {
    public void handleMessage(android.os.Message msg) {
    switch (msg.what) {
    case 0:
    isLogined.setText("登录失败");
    break;
    case 1:
    send.setVisibility(View.VISIBLE);
    message.setVisibility(View.VISIBLE);
    isLogined.setText("cnhua5登录成功!!!,可以与7inyu聊天了");
    break;
    case 2:
    Toast.makeText(_IM_oneActivity.this, receiveMsg.getBody(), 2000)
    .show();
    break;
    case 3:
    Log.e("****", "****" + msg.obj.toString());
    chat.append(msg.obj.toString() + "\n");
    break;
    default:
    break;
    }
    };
    };
     
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
     
    isLogined = (TextView) findViewById(R.id.isLogined);
    chat = (TextView) findViewById(R.id.chat);
    chat.setMovementMethod(ScrollingMovementMethod.getInstance());
    send = (Button) findViewById(R.id.send);
    send.setOnClickListener(this);
    message = (EditText) findViewById(R.id.msg);
     
    chatMana = XmppTool.getConnection().getChatManager();
    connectToServer();
    //receive message from '7inyu@sl-201112121136/Spark 2.6.3'
    receiveMFrom("7inyu@sl-201112121136/Spark 2.6.3");
    }
     
    /**
    * for send message to somebody if you want
    * @param to
    */
    private void receiveMFrom(String from){
    //chat with somebody
    newChat = chatMana.createChat(from, null);
    newChat.addMessageListener(new MessageListener() {
     
    @Override
    public void processMessage(Chat arg0, Message arg1) {
    // TODO Auto-generated method stub
    if(arg1.getTo().contains("cnhua5")){
    Log.e("from --->to..", arg1.getFrom() + "--给你发送消息了");
    android.os.Message msgs = new android.os.Message();
    msgs.obj = arg1.getFrom().split("@")[0] + "说:" + arg1.getBody();
    msgs.what = 3;
    handler.sendMessage(msgs);
    }
    }
    });
    }
     
    /**
    * connect to server
    */
    private void connectToServer() {
    new Thread(new Runnable() {
     
    @Override
    public void run() {
    // TODO Auto-generated method stub
    try {
    // login onto the server
    Log.e("--------------->", "logining...");
    XmppTool.getConnection().login("cnhua5", "cnhua5");
    Presence presence = new Presence(Presence.Type.available);
    XmppTool.getConnection().sendPacket(presence);
     
    Thread.sleep(2000);
     
    handler.sendEmptyMessage(1);
    } catch (Exception e) {
    // close connection if has some trouble
    Log.e("--------------->", "login failed...");
    XmppTool.closeConnection();
    try {
    Thread.sleep(2000);
    } catch (InterruptedException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    handler.sendEmptyMessage(0);
    e.printStackTrace();
    }
    }
    }).start();
    }
     
    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    if (v == send) {
    try {
    sendMsg = new Message();
    sendMsg.setBody(message.getText().toString());
    newChat.sendMessage(sendMsg);
    chat.append("你说:" + message.getText().toString() + "\n");
    } catch (XMPPException e) {
    e.printStackTrace();
    }
    message.setText("");
    }
    }
     
    @Override
    protected void onDestroy() {
    // TODO Auto-generated method stub
    XmppTool.closeConnection();
    super.onDestroy();
    }
    }
  • 相关阅读:
    一定要在3 20前完成所有的程序开发工作
    浅谈图像处理方向的就业前景[转)
    期待牛人指教的问题?
    vc6 7工程转vc8时的问题
    今天的工作计划
    定点数与浮点数区别
    difference between texRECT and tex2D
    Render to Texture
    不明白gluperpective的fovy参数
    批处理程序教程(转)
  • 原文地址:https://www.cnblogs.com/qiengo/p/2654566.html
Copyright © 2011-2022 走看看