public void run() { System.out.println(transfer.getStatus()); System.out.println(transfer.getProgress()); android.os.Message message = new android.os.Message(); // handle message.what = 3; while (!transfer.isDone()) { System.out.println(transfer.getStatus()); System.out.println(transfer.getProgress()); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (transfer.getStatus().equals(Status.error)) { msg.setReceive(MessageInfo.STATUS[2]); } else if (transfer.getStatus().equals(Status.refused)) { msg.setReceive(MessageInfo.STATUS[1]); } else { msg.setReceive(MessageInfo.STATUS[0]); // 成功 } handler.sendMessage(message); /* * System.out.println(transfer.getStatus()); * System.out.println(transfer.getProgress()); */ }
/** 从list 中取出 分拣名称相同的 Msg */ private MessageInfo queryMsgForListMsg(String filePath) { MessageInfo msg = null; for (int i = listMsg.size() - 1; i >= 0; i--) { msg = listMsg.get(i); if (filePath != null && filePath.contains(msg.getFilePath())) { // 对方传过来的只是文件的名称 return msg; } } return msg; }
@Override public MessageInfo createFromParcel(Parcel source) { MessageInfo messageInfo = new MessageInfo(); messageInfo.userid = source.readString(); messageInfo.msg = source.readString(); messageInfo.date = source.readString(); messageInfo.from = source.readString(); messageInfo.type = source.readString(); messageInfo.receive = source.readString(); messageInfo.time = source.readString(); messageInfo.filePath = source.readString(); return messageInfo; }
public void handleMessage(android.os.Message msg) { switch (msg.what) { case 1: MessageInfo chatMsg = MessageInfo.analyseMsgBody(msg.obj.toString()); if (chatMsg != null) { listMsg.add(chatMsg); // 添加到聊天消息 adapter.notifyDataSetChanged(); } break; case 2: // 发送文件 break; case 3: // 更新文件发送状态 adapter.notifyDataSetChanged(); break; case 5: // 接收文件 MessageInfo msg2 = (MessageInfo) msg.obj; System.out.println(msg2.getFrom()); listMsg.add(msg2); adapter.notifyDataSetChanged(); default: break; } };
/** * 分析消息内容 * * @param body Json */ public static MessageInfo analyseMsgBody(String jsonStr) { MessageInfo msg = new MessageInfo(); // 获取用户、消息、时间、IN try { JSONObject jsonObject = new JSONObject(jsonStr); msg.setUserid(jsonObject.getString(MessageInfo.USERID)); msg.setFrom(jsonObject.getString(MessageInfo.FROM)); msg.setMsg(jsonObject.getString(MessageInfo.MSG_CONTENT)); msg.setDate(jsonObject.getString(MessageInfo.DATE)); msg.setType(jsonObject.getString(MessageInfo.MSG_TYPE)); msg.setReceive(jsonObject.getString(MessageInfo.RECEIVE_STAUTS)); msg.setTime(jsonObject.getString(MessageInfo.TIME_REDIO)); msg.setFilePath(jsonObject.getString(MessageInfo.FIL_PAHT)); } catch (JSONException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } finally { return msg; } }
/** 传json */ public static String toJson(MessageInfo msg) { JSONObject jsonObject = new JSONObject(); String jsonStr = ""; try { jsonObject.put(MessageInfo.USERID, msg.getUserid() + ""); jsonObject.put(MessageInfo.MSG_CONTENT, msg.getMsg() + ""); jsonObject.put(MessageInfo.DATE, msg.getDate() + ""); jsonObject.put(MessageInfo.FROM, msg.getFrom() + ""); jsonObject.put(MessageInfo.MSG_TYPE, msg.getType() + ""); jsonObject.put(MessageInfo.RECEIVE_STAUTS, msg.getReceive() + ""); jsonObject.put(MessageInfo.TIME_REDIO, msg.getTime()); jsonObject.put(MessageInfo.FIL_PAHT, msg.getFilePath()); jsonStr = jsonObject.toString(); Log.d("msg json", jsonStr + ""); } catch (JSONException e) { e.printStackTrace(); } finally { return jsonStr; } }