Google Places API。 PlaceLikelihoodBuffer响应缓慢。 Android的

时间:2015-06-25 06:45:41

标签: android performance google-places-api

我正在使用PlaceLikelihoodBuffer buffer = result.await();(在AsyncTask中),以获得我目前最有可能找到的地方。

代码完美无缺,但响应非常慢,尤其是我第一次打开应用程序并运行搜索/方法时

第一次获得回复通常需要3秒钟。所以我的问题是这是否正常?还是假设要快得多?在那种情况下,可能是我缓慢的可能原因。 (ps。GoogleApiClient连接非常快。PlaceDetectionAPIclient连接时运行。)

代码:

以下是Api Client

   mGoogleApiClient = new GoogleApiClient.Builder(activity)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)                  
            .addApi(Places.PLACE_DETECTION_API)
            .addApi(Places.GEO_DATA_API)
            .build();

    mGoogleApiClient.blockingConnect();

以下是致电Placelikelyhood

PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi.getCurrentPlace(mGoogleApiClient, null);
PlaceLikelihoodBuffer buffer = result.await();

1 个答案:

答案 0 :(得分:0)

您正在使用mGoogleApiClient.blockingConnect(),它必须在后台线程中运行,并且您正在使用PendingResult.await(),它也必须在后台线程中运行。

使用后台线程的额外开销似乎可能是额外延迟的原因。

请注意,另一个原因可能是数据连接的速度。

而且,返回结果需要多长时间的另一个原因是结果中有多少项基于当前位置。

我只是使用回调和没有后台线程来测试它,并使用不同的数据连接类型测试了第一次启动行为(我确保在启动之前从任务列表中删除它)。

我使用connect()代替blockingConnect()

mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Places.PLACE_DETECTION_API)
            .addApi(Places.GEO_DATA_API)
            .build();

    //mGoogleApiClient.blockingConnect();
    mGoogleApiClient.connect();

然后在onConnected()我使用标准回调而不是PendingResult.await()

@Override
public void onConnected(Bundle bundle) {

    Log.i(TAG, "mGoogleApiClient connected");

    PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi
            .getCurrentPlace(mGoogleApiClient, null);
    result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
        @Override
        public void onResult(PlaceLikelihoodBuffer likelyPlaces) {
            for (PlaceLikelihood placeLikelihood : likelyPlaces) {
                Log.i(TAG, String.format("Place '%s' has likelihood: %g",
                        placeLikelihood.getPlace().getName(),
                        placeLikelihood.getLikelihood()));
            }
            likelyPlaces.release();
        }
    });
}

首次测试,连接到我的2.5 GHz WiFi连接。 在日志中,您可以看到它在26:11.239处发出请求,结果显示在26:12.920,这是1.681秒的差异:

06-25 00:26:11.239  20257-20257/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ mGoogleApiClient connected
06-25 00:26:12.920  20257-20257/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'Tocher Services Inc' has likelihood: 0.250000
//........

接下来的测试,我连接到我的5 GHz WiFi连接,它在1.521秒内回来了:

06-25 00:51:24.385  24193-24193/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ mGoogleApiClient connected
06-25 00:51:25.906  24193-24193/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'Tocher Services Inc' has likelihood: 0.250000

接下来的测试,与WiFi断开连接,连接到LTE。

这不奇怪地花了一点时间:2.642秒。

然而,令人惊讶的是,与连接到WiFi相比,它的结果要多得多(之前的结果每次都有4个结果,之前的日志中没有显示,这里我列出了完整列表):

06-25 00:57:45.767  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ mGoogleApiClient connected
06-25 00:57:48.409  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'Navlet's Garden Center' has likelihood: 0.250000
06-25 00:57:48.409  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'Safeway' has likelihood: 0.0900000
06-25 00:57:48.419  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'Caffino' has likelihood: 0.0800000
06-25 00:57:48.419  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'CVS Pharmacy' has likelihood: 0.0700000
06-25 00:57:48.419  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'Devon Apartments' has likelihood: 0.0600000
06-25 00:57:48.419  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'Strawberry Fields DJ Co' has likelihood: 0.0500000
06-25 00:57:48.419  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'Yanni's Greek Cafe' has likelihood: 0.0400000
06-25 00:57:48.419  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'Mother India' has likelihood: 0.0300000
06-25 00:57:48.419  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'Valero' has likelihood: 0.0200000
06-25 00:57:48.429  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place '76' has likelihood: 0.0100000
06-25 00:57:48.429  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'The UPS Store' has likelihood: 0.0100000
06-25 00:57:48.429  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'Chateau Pleasant Hill' has likelihood: 0.0100000
06-25 00:57:48.429  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'Carland' has likelihood: 0.0100000
06-25 00:57:48.429  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'Chateau Pleasant Hill' has likelihood: 0.0100000
06-25 00:57:48.429  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'El Mariachi Mexican Grill' has likelihood: 0.0100000
06-25 00:57:48.429  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'Alhambra Hills Realty' has likelihood: 0.0100000
06-25 00:57:48.429  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'Starbucks' has likelihood: 0.0100000
06-25 00:57:48.429  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'Stepping Stones Learning Center' has likelihood: 0.0100000
06-25 00:57:48.429  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'Intercontinental Services' has likelihood: 0.0100000
06-25 00:57:48.429  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'Fitness Evolution' has likelihood: 0.0100000