【问题标题】:Does public static AutocompleteSessionToken newInstance () return same instance?public static AutocompleteSessionToken newInstance() 是否返回相同的实例?
【发布时间】:2020-04-22 18:14:07
【问题描述】:

此方法是否在单例模式下运行?还是每次都为用户创建新会话?

在edittext更改监听器上调用以下方法。

   @NonNull
   private ArrayList<PlaceAutocomplete> getPredictions(@NonNull CharSequence constraint) {

       final ArrayList<PlaceAutocomplete> resultList = new ArrayList<>();

       // Create a new token for the autocomplete session. Pass this to FindAutocompletePredictionsRequest,
       // and once again when the user makes a selection (for example when calling fetchPlace()).
       AutocompleteSessionToken token = AutocompleteSessionToken.newInstance();

       //https://gist.github.com/graydon/11198540
       // Use the builder to create a FindAutocompletePredictionsRequest.
       FindAutocompletePredictionsRequest request = FindAutocompletePredictionsRequest.builder()
              .setLocationBias(bounds)
               .setCountry("PK")
               .setSessionToken(token)
               .setQuery(constraint.toString())
               .build();

       Task<FindAutocompletePredictionsResponse> autocompletePredictions = placesClient.findAutocompletePredictions(request);

       // This method should have been called off the main UI thread. Block and wait for at most
       // 60s for a result from the API.
       try {
           Tasks.await(autocompletePredictions, 60, TimeUnit.SECONDS);
       } catch (@NonNull ExecutionException | InterruptedException | TimeoutException e) {
           e.printStackTrace();
       }

       if (autocompletePredictions.isSuccessful()) {
           FindAutocompletePredictionsResponse findAutocompletePredictionsResponse = autocompletePredictions.getResult();
           if (findAutocompletePredictionsResponse != null)
               for (AutocompletePrediction prediction : findAutocompletePredictionsResponse.getAutocompletePredictions()) {
                   Log.i(TAG, prediction.getPlaceId());
                   resultList.add(new PlaceAutocomplete(prediction.getPlaceId(), prediction.getPrimaryText(STYLE_NORMAL).toString(), prediction.getFullText(STYLE_BOLD).toString()));
               }

           return resultList;
       } else {
           return resultList;
       }

   }

该方法会在 editText 中的每个文本更改时调用。

【问题讨论】:

    标签: java android google-maps google-places-api googleplacesautocomplete


    【解决方案1】:

    AutocompleteSessionToken.newInstance() 方法返回一个新实例,即一个新的会话令牌。每次调用此方法时,您都是creating 一个新的会话令牌。

    Google 解释了自动完成会话的工作原理here

    会话令牌对用户的查询和选择阶段进行分组 出于计费目的自动完成搜索到离散会话中。这 会话在用户开始输入查询时开始,并在 他们选择了一个地方。每个会话可以有多个查询,其次是 通过一个地方选择。会话结束后,令牌不会 有效期更长;您的应用必须为每个会话生成一个新令牌。

    当用户从自动完成预测中选择一个地点时,editText 中的文本会发生变化(会话结束,当用户选择一个新地点时将创建一个新地点)。

    希望这会有所帮助!

    【讨论】:

    • 感谢@evan,我们得到了这个……通过打印日志……它重新创建了实例
    • 很高兴听到! :)
    猜你喜欢
    • 2014-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多