本文转自:
复制代码
http://www.apkbus.com/forum.php?mod=viewthread&tid=121249&highlight=smack
1. 登陆界面 MainActivity.java
- package com.example.openfire_example;
- import org.jivesoftware.smack.XMPPException;
- import org.jivesoftware.smack.packet.Presence;
- import android.os.Build;
- import android.os.Bundle;
- import android.os.StrictMode;
- import android.annotation.SuppressLint;
- import android.annotation.TargetApi;
- import android.app.Activity;
- import android.content.Intent;
- import android.util.Log;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.Window;
- import android.widget.CheckBox;
- import android.widget.EditText;
- import android.widget.Toast;
- @SuppressLint("NewApi")
- public class MainActivity extends Activity{
- private EditText accountEditText;
- private EditText passwordEditText;
- private CheckBox remeberCheckBox;
-
- // @TargetApi(Build.VERSION_CODES.GINGERBREAD)
- // @SuppressLint("NewApi")
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.activity_main);
- StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
- StrictMode.setThreadPolicy(policy);
- accountEditText=(EditText) findViewById(R.id.et_account);
- passwordEditText=(EditText) findViewById(R.id.et_password);
- remeberCheckBox=(CheckBox) findViewById(R.id.login_remember);
-
- findViewById(R.id.btn_cancl).setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View v) {
- finish();
- }
-
- });
-
- findViewById(R.id.btn_login).setOnClickListener(new OnClickListener(){
- public void onClick(View v) {
- String account=accountEditText.getText().toString();
- String password=passwordEditText.getText().toString();
- if(account.equals("") || password.equals("")){
- Toast.makeText(MainActivity.this, "账号或密码不能为空!", Toast.LENGTH_SHORT).show();
- }else{
- // ClientConServer ccs=new ClientConServer(MainActivity.this);
- // boolean b=ccs.login(account,password);
- try{
- XmppTool.getConnection().login(account,password);
- // Log.i("XMPPClient", "Logged in as " + XmppTool.getConnection().getUser());
-
- //״̬
- Presence presence = new Presence(Presence.Type.available);
- XmppTool.getConnection().sendPacket(presence);
- //如果登录成功
- // if(b){
- Toast.makeText(MainActivity.this, "登陆成功!", Toast.LENGTH_SHORT).show();
-
- startActivity(new Intent(MainActivity.this,LoginSuccessfulActivity.class));
- Log.i("", "登陆成功!登陆成功!登陆成功!登陆成功!登陆成功!");
- LoginSuccessfulActivity.account=account;
- }catch(XMPPException e)
- {
- XmppTool.closeConnection();
-
- }
- // }else{
- // Toast.makeText(MainActivity.this, "登陆失败!", Toast.LENGTH_SHORT).show();
- // }
- }
- }
- });
- }
-
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
-
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.activity_main, menu);
- return true;
- }
-
- }
2. 服务界面 XmppTool.java
复制代码
3. 聊天界面 FormClient.java
复制代码
因为这个项目也是比较匆忙就没有讲那么完善,注册跟好友获取没有,需要的朋友留言或者私聊我 jar包也在源码里
- public class XmppTool {
- private static XMPPConnection con = null;
- private static void openConnection() {
- try {
- //声明XMPP连接 ConnectionConfiguration类的三个参数分别为ip地址,端口号
- ConnectionConfiguration connConfig = new ConnectionConfiguration("192.168.159.1", 5222);
- con = new XMPPConnection(connConfig);
- //开始连接
- con.connect();
- }
- catch (XMPPException xe)
- {
- xe.printStackTrace();
- }
- }
- public static XMPPConnection getConnection() {
- if (con == null) {
- openConnection();
- }
- return con;
- }
- public static void closeConnection() {
- con.disconnect();
- con = null;
- }
- public class FormClient extends Activity {
- private MyAdapter adapter;
- private List<Msg> listMsg = new ArrayList<Msg>();
- private String pUSERID;
- private EditText msgText;
- private ProgressBar pb;
- public class Msg {
- String userid;
- String msg;
- String date;
- String from;
- public Msg(String userid, String msg, String date, String from) {
- this.userid = userid;
- this.msg = msg;
- this.date = date;
- this.from = from;
- }
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.formclient);
-
- //获取itent传送的用户名
- this.pUSERID = getIntent().getStringExtra("USERID");
-
- ListView listview = (ListView) findViewById(R.id.formclient_listview);
- listview.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
-
- this.adapter = new MyAdapter(this);
- listview.setAdapter(adapter);
-
- //获取用户输入的聊天信息
- this.msgText = (EditText) findViewById(R.id.formclient_text);
- this.pb = (ProgressBar) findViewById(R.id.formclient_pb);
- //建立聊天
- ChatManager cm = XmppTool.getConnection().getChatManager();
- //监听好友发送的聊天消息
- final Chat newchat = cm.createChat("test@im.comit.com.cn", null);
-
- cm.addChatListener(new ChatManagerListener() {
- @Override
- public void chatCreated(Chat chat, boolean able)
- {
- chat.addMessageListener(new MessageListener() {
- @Override
- public void processMessage(Chat chat2, Message message)
- {
- //接受聊天消息 如果是这个好友发送的
- if(message.getFrom().contains("test@im.comit.com.cn"))
- {
- //显示好友发送的消息
- String[] args = new String[] { "test", message.getBody(), TimeRender.getDate(), "IN" };
-
- //将好友的消息显示到界面
- android.os.Message msg = handler.obtainMessage();
- msg.what = 1;
- msg.obj = args;
- msg.sendToTarget();
- }
- else
- {
- //message.getFrom().cantatins("test@im.comit.com.cn);
- }
-
- }
- });
- }
- });
- //监听发送按钮
- Button btsend = (Button) findViewById(R.id.formclient_btsend);
- btsend.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- //获取发送内容
- String msg = msgText.getText().toString();
-
- if(msg.length() > 0){
- //显示发送内容
- listMsg.add(new Msg(pUSERID, msg, TimeRender.getDate(), "OUT"));
- //刷新发送的消息到界面
- adapter.notifyDataSetChanged();
-
- try {
- //发送消息
- newchat.sendMessage(msg);
-
- }
- catch (XMPPException e)
- {
- e.printStackTrace();
- }
- }
- else
- {
- Toast.makeText(FormClient.this, "发送消息失败", Toast.LENGTH_SHORT).show();
- }
- //清空发送消息输入框
- msgText.setText("");
- }
- });
-
-
- FileTransferManager fileTransferManager = new FileTransferManager(XmppTool.getConnection());
- fileTransferManager.addFileTransferListener(new RecFileTransferListener());
- }
-
-
-
- private FileTransferRequest request;
- private File file;
- class RecFileTransferListener implements FileTransferListener
- {
- @Override
- public void fileTransferRequest(FileTransferRequest prequest)
- {
-
- // System.out.println("The file received from: " + prequest.getRequestor());
-
- file = new File("mnt/sdcard/" + prequest.getFileName());
- request = prequest;
- handler.sendEmptyMessage(5);
- }
- }
- private Handler handler = new Handler() {
- public void handleMessage(android.os.Message msg)
- {
-
- switch (msg.what) {
- case 1:
- //显示会话消息
- String[] args = (String[]) msg.obj;
- listMsg.add(new Msg(args[0], args[1], args[2], args[3]));
- //刷新界面显示消息
- adapter.notifyDataSetChanged();
- break;
- default:
- break;
- }
- };
- };
- //返回按钮
- @Override
- public void onBackPressed()
- {
- super.onBackPressed();
- XmppTool.closeConnection();
- System.exit(0);
- android.os.Process.killProcess(android.os.Process
- .myPid());
- }
- class MyAdapter extends BaseAdapter {
- private Context cxt;
- private LayoutInflater inflater;
- public MyAdapter(FormClient formClient) {
- this.cxt = formClient;
- }
- @Override
- public int getCount() {
- return listMsg.size();
- }
- @Override
- public Object getItem(int position) {
- return listMsg.get(position);
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent)
- {
- this.inflater = (LayoutInflater) this.cxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
-
- //IN,OUT消息
- if(listMsg.get(position).from.equals("IN"))
- {
- convertView = this.inflater.inflate(R.layout.formclient_chat_in, null);
- }
- else
- {
- convertView = this.inflater.inflate(R.layout.formclient_chat_out, null);
- }
-
- TextView useridView = (TextView) convertView.findViewById(R.id.formclient_row_userid);
- TextView dateView = (TextView) convertView.findViewById(R.id.formclient_row_date);
- TextView msgView = (TextView) convertView.findViewById(R.id.formclient_row_msg);
-
- useridView.setText(listMsg.get(position).userid);
- dateView.setText(listMsg.get(position).date);
- msgView.setText(listMsg.get(position).msg);
-
- return convertView;
- }
- }
- }
应观众要求上运行图:
-
3.03 MB, 下载次数: 2251