成功和失败侦听器方法是否在后台线程上完成?

Are success and failure listener methods done on a background thread?

使用成功和失败侦听器的方法是在主 UI 线程还是后台线程上完成的?

例如,我正在使用 Google Places SDK。获取位置:

// Define a Place ID.
final String placeId = "INSERT_PLACE_ID_HERE";

// Specify the fields to return.
final List<Place.Field> placeFields = Arrays.asList(Place.Field.ID, Place.Field.NAME);

// Construct a request object, passing the place ID and fields array.
final FetchPlaceRequest request = FetchPlaceRequest.newInstance(placeId, placeFields);

placesClient.fetchPlace(request).addOnSuccessListener((response) -> {
    Place place = response.getPlace();
    Log.i(TAG, "Place found: " + place.getName());
}).addOnFailureListener((exception) -> {
    if (exception instanceof ApiException) {
        final ApiException apiException = (ApiException) exception;
        Log.e(TAG, "Place not found: " + exception.getMessage());
        final int statusCode = apiException.getStatusCode();
        // TODO: Handle error with given status code.
    }
});

fetchPlace() 是在后台线程中完成的吗?

啊,我做了一些挖掘,这就是我想出的。

placesClient.fetchPlace(...) returns一个Task:

这对我们来说意味着您可以将它连接到各种侦听器并在达到该状态时获得 ping。对于您选择的特定 addOnSuccessListener(...) 方法,以下是文档告诉我们的内容:

所以基本上,在您的代码片段中,提取将在主线程之外完成,然后结果将返回到主线程。