有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

使用成功和失败侦听器的方法是在主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()是在后台线程中完成的吗


共 (1) 个答案

  1. # 1 楼答案

    啊,我做了一些挖掘,下面是我得出的结论

    placesClient.fetchPlace(...)返回一个Task

    Task definition

    这对我们来说意味着,你可以把它连接到不同的监听器上,每当这个状态被点击时,就会得到一个ping。对于你选择的那种特定addOnSuccessListener(...)方法,以下是文档告诉我们的:

    addOnSuccessListener(...) definition

    所以基本上,在代码片段中,抓取将在主线程之外完成,然后结果将被传递回主线程