/** 根据conversationId查询消息记录 */ public List<MessageInfo> queryMessageInfosByFromIdAndToId( String fromId, String toId, int num, int startIndex) { MessageInfo msg = null; List<MessageInfo> msgList = new ArrayList<MessageInfo>(); Cursor c = null; SQLiteDatabase dbSlaver = null; try { dbSlaver = helper.getReadableDatabase(); String querySql = "select * from t_message" + " where (fromuserid like ? and touserid like ?) or (touserid like ? and fromuserid like ?) order by mid desc"; if (num > 0 && startIndex >= 0) { querySql = querySql + "limit " + num + " offset " + startIndex; } c = dbSlaver.rawQuery( querySql, new String[] {fromId + "%", toId + "%", fromId + "%", toId + "%"}); if (c != null && c.getCount() > 0) { while (c.moveToNext()) { msg = new MessageInfo(); msg.setMid(c.getInt(c.getColumnIndex("mid"))); msg.setMessageId(c.getString(c.getColumnIndex(IMDbHelper.MESSAGE_ID))); msg.setFromuserId(c.getString(c.getColumnIndex(IMDbHelper.FROM_USER_ID))); msg.setTouserId(c.getString(c.getColumnIndex(IMDbHelper.TO_USER_ID))); msg.setIsRequireEncryption(c.getInt(c.getColumnIndex(IMDbHelper.IS_REQUIRE_ENCRYPTION))); msg.setIsEncryptedOnServer(c.getInt(c.getColumnIndex(IMDbHelper.IS_ENCRYPTED_ON_SERVER))); msg.setIsRead(c.getInt(c.getColumnIndex(IMDbHelper.IS_READ))); msg.setTimesTamp(c.getString(c.getColumnIndex(IMDbHelper.TIMES_TAMP))); msg.setIsReadacked(c.getInt(c.getColumnIndex(IMDbHelper.IS_READACKED))); msg.setIsDeliveredacked(c.getInt(c.getColumnIndex(IMDbHelper.IS_DELIVERED_HACKED))); msg.setContentType(c.getInt(c.getColumnIndex(IMDbHelper.CONTENT_TYPE))); msg.setContent(c.getString(c.getColumnIndex(IMDbHelper.CONTENT))); msg.setConversationId(c.getString(c.getColumnIndex(IMDbHelper.CONVERSATION_ID))); msg.setSenderName(c.getString(c.getColumnIndex(IMDbHelper.SENDER_NAME))); msg.setExt(c.getString(c.getColumnIndex(IMDbHelper.EXT))); msg.setDeliveryState(c.getInt(c.getColumnIndex(IMDbHelper.DELIVERY_STATE))); msg.setIsAnonymous(c.getInt(c.getColumnIndex(IMDbHelper.IS_ANONYMOUS))); msg.setMessageType(c.getInt(c.getColumnIndex(IMDbHelper.MESSAGE_TYPE))); msg.setPortraitImg(c.getString(c.getColumnIndex(IMDbHelper.PORTRAIT_IMG))); msg.setDuration(c.getInt(c.getColumnIndex(IMDbHelper.DURATION))); msg.setAudioFilePath(c.getString(c.getColumnIndex(IMDbHelper.AUDIO_FILE_PATH))); msgList.add(msg); } if (null != c) { c.close(); c = null; } } } catch (Exception e) { LogUtil.info(e.toString()); } Collections.reverse(msgList); return msgList; }
/** 查询最后一条聊天记录 */ public MessageInfo queryMessageInfoByFromIdAndToId(String fromId, String toId) { MessageInfo msg = null; Cursor c = null; SQLiteDatabase dbSlaver = null; try { dbSlaver = helper.getReadableDatabase(); String querySql = "select * from t_message where (fromuserid like ? and touserid like ?) or (touserid like ? and fromuserid like ?) order by mid desc limit 1"; c = dbSlaver.rawQuery( querySql, new String[] {fromId + "%", toId + "%", fromId + "%", toId + "%"}); if (c != null && c.moveToFirst()) { msg = new MessageInfo(); msg.setMid(c.getInt(c.getColumnIndex("mid"))); msg.setMessageId(c.getString(c.getColumnIndex("messageid"))); msg.setFromuserId(c.getString(c.getColumnIndex("fromuserid"))); msg.setTouserId(c.getString(c.getColumnIndex("touserid"))); msg.setIsRequireEncryption(c.getInt(c.getColumnIndex("isrequireencryption"))); msg.setIsEncryptedOnServer(c.getInt(c.getColumnIndex("isencryptedonserver"))); msg.setIsRead(c.getInt(c.getColumnIndex("isRead"))); msg.setTimesTamp(c.getString(c.getColumnIndex("timestamp"))); msg.setIsReadacked(c.getInt(c.getColumnIndex("isreadacked"))); msg.setIsDeliveredacked(c.getInt(c.getColumnIndex("isdeliveredacked"))); msg.setContentType(c.getInt(c.getColumnIndex("contenttype"))); msg.setContent(c.getString(c.getColumnIndex("content"))); msg.setConversationId(c.getString(c.getColumnIndex("conversationid"))); msg.setSenderName(c.getString(c.getColumnIndex("sendername"))); msg.setExt(c.getString(c.getColumnIndex("ext"))); msg.setDeliveryState(c.getInt(c.getColumnIndex("deliverystate"))); msg.setIsAnonymous(c.getInt(c.getColumnIndex("isanonymous"))); msg.setMessageType(c.getInt(c.getColumnIndex("messagetype"))); msg.setPortraitImg(c.getString(c.getColumnIndex("portraitimg"))); } } catch (SQLException e) { LogUtil.info(e.toString()); } finally { if (null != c) { c.close(); } } return msg; }