public static AutocompleteSessionToken newInstance () return 是同一个实例吗?
Does public static AutocompleteSessionToken newInstance () return same instance?
这个方法是否适用于单例模式?还是每次都为用户创建新会话?
在 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 中的每个文本更改。
AutocompleteSessionToken.newInstance()
方法returns一个新的实例,即一个新的会话令牌。每次调用此方法时,您都是 creating 一个新的会话令牌。
Google 解释了自动完成会话的工作原理 here:
Session tokens group the query and selection phases of a user
autocomplete search into a discrete session for billing purposes. The
session begins when the user starts typing a query, and concludes when
they select a place. Each session can have multiple queries, followed
by one place selection. Once a session has concluded, the token is no
longer valid; your app must generate a fresh token for each session.
当用户从自动完成预测中选择一个地点时,editText 中的文本会发生变化(会话结束,当用户选择一个新地点时将创建一个新文本)。
希望对您有所帮助!
这个方法是否适用于单例模式?还是每次都为用户创建新会话?
在 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 中的每个文本更改。
AutocompleteSessionToken.newInstance()
方法returns一个新的实例,即一个新的会话令牌。每次调用此方法时,您都是 creating 一个新的会话令牌。
Google 解释了自动完成会话的工作原理 here:
Session tokens group the query and selection phases of a user autocomplete search into a discrete session for billing purposes. The session begins when the user starts typing a query, and concludes when they select a place. Each session can have multiple queries, followed by one place selection. Once a session has concluded, the token is no longer valid; your app must generate a fresh token for each session.
当用户从自动完成预测中选择一个地点时,editText 中的文本会发生变化(会话结束,当用户选择一个新地点时将创建一个新文本)。
希望对您有所帮助!