public void queryMessages(final int limit, final AVIMMessagesQueryCallback callback) {
    if (limit <= 0 || limit > 1000) {
      if (callback != null) {
        callback.internalDone(null, new AVException(new IllegalArgumentException(
                "limit should be in [1, 1000]")));
      }
    }
    // 如果屏蔽了本地缓存则全部走网络
    if (!AVIMOptions.getGlobalOptions().isMessageQueryCacheEnabled()) {
      queryMessagesFromServer(null, 0, limit, null, 0, new AVIMMessagesQueryCallback() {

        @Override
        public void done(List<AVIMMessage> messages, AVIMException e) {
          if (callback != null) {
            if (e != null) {
              callback.internalDone(e);
            } else {
              callback.internalDone(messages, null);
            }
          }
        }
      });
      return;
    }
    if (!AppConfiguration.getGlobalNetworkingDetector().isConnected()) {
      queryMessagesFromCache(null, 0, limit, callback);
    } else {
      // 选择最后一条有 breakpoint 为 false 的消息做截断，因为是 true 的话，会造成两次查询。
      // 在 queryMessages 还是遇到 breakpoint，再次查询了
      long cacheMessageCount = storage.getMessageCount(conversationId);
      long toTimestamp = 0;
      String toMsgId = null;
      // 如果本地的缓存的量都不够的情况下，应该要去服务器查询，以免第一次查询的时候出现limit跟返回值不一致让用户认为聊天记录已经到头的问题
      if (cacheMessageCount >= limit) {
        final AVIMMessage latestMessage =
                storage.getLatestMessageWithBreakpoint(conversationId, false);

        if (latestMessage != null) {
          toMsgId = latestMessage.getMessageId();
          toTimestamp = latestMessage.getTimestamp();
        }
      }

      // 去服务器查询最新消息，看是否在其它终端产生过消息。为省流量，服务器会截断至 toMsgId 、toTimestamp
      queryMessagesFromServer(null, 0, limit, toMsgId, toTimestamp,
              new AVIMMessagesQueryCallback() {
                @Override
                public void done(List<AVIMMessage> messages, AVIMException e) {
                  if (e != null) {
                    // 如果遇到本地错误或者网络错误，直接返回缓存数据
                    if (e.getCode() == AVIMException.TIMEOUT || e.getCode() == 0 || e.getCode() == 3000) {
                      queryMessagesFromCache(null, 0, limit, callback);
                    } else {
                      if (callback != null) {
                        callback.internalDone(e);
                      }
                    }
                  } else {
                    if (null == messages || messages.size() < 1) {
                      // 这种情况就说明我们的本地消息缓存是最新的
                    } else {
                      /*
                       * 1.messages.size()<=limit && messages.contains(latestMessage)
                       * 这种情况就说明在本地客户端退出后，该用户在其他客户端也产生了聊天记录而没有缓存到本地来,且产生了小于一页的聊天记录
                       * 2.messages==limit && !messages.contains(latestMessage)
                       * 这种情况就说明在本地客户端退出后，该用户在其他客户端也产生了聊天记录而没有缓存到本地来,且产生了大于一页的聊天记录
                       */

                      processContinuousMessages(messages);
                    }
                    queryMessagesFromCache(null, 0, limit, callback);
                  }
                }
              });
    }
  }
public void queryMessages ( final int limit , final AVIMMessagesQueryCallback callback ) { if ( limit <= 0 || limit > 1000 ) { if ( callback != null ) { callback . internalDone ( null , new AVException ( new IllegalArgumentException ( "limit should be in [1, 1000]" ) ) ) ; } } if ( ! AVIMOptions . getGlobalOptions ( ) . isMessageQueryCacheEnabled ( ) ) { queryMessagesFromServer ( null , 0 , limit , null , 0 , new AVIMMessagesQueryCallback ( ) { @ Override public void done ( List < AVIMMessage > messages , AVIMException e ) { if ( callback != null ) { if ( e != null ) { callback . internalDone ( e ) ; } else { callback . internalDone ( messages , null ) ; } } } } ) ; return ; } if ( ! AppConfiguration . getGlobalNetworkingDetector ( ) . isConnected ( ) ) { queryMessagesFromCache ( null , 0 , limit , callback ) ; } else { long cacheMessageCount = storage . getMessageCount ( conversationId ) ; long toTimestamp = 0 ; String toMsgId = null ; if ( cacheMessageCount >= limit ) { final AVIMMessage latestMessage = storage . getLatestMessageWithBreakpoint ( conversationId , false ) ; if ( latestMessage != null ) { toMsgId = latestMessage . getMessageId ( ) ; toTimestamp = latestMessage . getTimestamp ( ) ; } } queryMessagesFromServer ( null , 0 , limit , toMsgId , toTimestamp , new AVIMMessagesQueryCallback ( ) { @ Override public void done ( List < AVIMMessage > messages , AVIMException e ) { if ( e != null ) { if ( e . getCode ( ) == AVIMException . TIMEOUT || e . getCode ( ) == 0 || e . getCode ( ) == 3000 ) { queryMessagesFromCache ( null , 0 , limit , callback ) ; } else { if ( callback != null ) { callback . internalDone ( e ) ; } } } else { if ( null == messages || messages . size ( ) < 1 ) { } else { processContinuousMessages ( messages ) ; } queryMessagesFromCache ( null , 0 , limit , callback ) ; } } } ) ; } }
获取最新的消息记录

@param limit
@param callback