8f69232c9d0a0d87c07ac8f491b37fbb68285957.svn-base
3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.ivt.android.me.utils.xmpp;
import java.io.File;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smackx.filetransfer.FileTransferManager;
import org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer;
import android.util.Log;
public class XmppSendMessage {
private static String TAG = "XmppMessage";
private XMPPConnection con = XmppConnectionTool.getInstance()
.getConnection();
public static XmppSendMessage xcl = new XmppSendMessage();
private boolean isChatAuth = false;
public static XmppSendMessage getInstance() {
return xcl;
}
/**
* Description: 发送单人聊天信息
*
* @param chat
* @param chatContent
* @return
*/
public boolean sendSignleChat(Chat chat, String chatContent) {
try {
chat.sendMessage(chatContent);
return true;
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
// /**
// * Description: 发送群组聊天信息
// *
// * @param chat
// * @param chatContent
// * @return
// */
// public int sendMultiChat(MultiUserChat muc, MeMessage msg) {
// String msgAgo = JSON.toJSONString(memAgo);
// String jsonStr = JSON.toJSONString(msg);
// if (msgAgo.equals(jsonStr)) {
// return 0;
// } else {
// memAgo = msg;
// }
// if (isChatAuth) {
// if (msg.getMsgType().equals(XmppMessageType.TYPE_MSG)
// || msg.getMsgType().equals(XmppMessageType.TYPE_BAR)) {
// return 2;//已被禁言
// }
// }
// if (!"".equals(jsonStr)) {
// try {
// muc.sendMessage(jsonStr);
// return 1;
// } catch (XMPPException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
// return 0;
// }
/**
* Description: 发送广播
*
* @param xmlString
* @return
*/
public boolean sendBroadcast(String xmlString) {
if (con.isConnected() && con.getUser() != null) {
Message message = new Message("all@broadcast.ivt");
// Message message = new Message(null);
message.setBody(xmlString);
con.sendPacket(message);
return true;
}
return false;
}
/**
* Description: 发送文件
*
* @param userId
* @param conn
* @param file
* @throws Exception
*/
public void sendFile(String userId, File file) throws Exception {
FileTransferManager manager = new FileTransferManager(con);
OutgoingFileTransfer transfer = manager
.createOutgoingFileTransfer(userId);
Log.i(TAG, "要发送的文件是否存在:" + file.exists() + "***" + file.getPath());
if (file.exists()) {
transfer.sendFile(file, "data backup");
}
Log.i(TAG, transfer.getStatus() + "**1");// 开始发送(Initial)
while (!transfer.isDone()) {
// Thread.sleep(1000*2);
Log.i(TAG, "等待对方接收");
if (transfer.getStatus() == OutgoingFileTransfer.Status.in_progress) {
// 可以调用transfer.getProgress();获得传输的进度
Log.i(TAG, "发送文件:" + transfer.getProgress() + "***\n"
+ transfer.getStatus());// 发送中(In Progress)
}
}
Log.i(TAG, transfer.getStatus() + "**2");// 完成(Complete) 拒绝(Refused)
}
}