LocationServices.FusedLocationApi.requestLocationUpdates onLocationChanged未被回调

时间:2015-08-07 09:41:16

标签: android gps google-play-services

我试图每1分钟或100米距离获得GPS位置。但onChangeLocation永远不会根据设备上的给定约束来回调。

我在代码中做了什么错误

清单中的

权限

** uses-permission android:name =“android.permission.ACCESS_FINE_LOCATION”**

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;


    @Overrid
    public void onCreate() {

            this.googleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(LocationServices.API)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();

            LOCATION_REQUEST =
                    LocationRequest.create().setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY).setFastestInterval(1000).
                            setExpirationTime(1000);

            this.googleApiClient.connect();
    }

    @Override
    public void onConnected(Bundle bundle) {

        isGoogleClientConnected = true;
        if (this.googleApiClient.hasConnectedApi(LocationServices.API)) {
            Toast.makeText(getApplicationContext(), "LocationServices is available.", Toast.LENGTH_LONG).show();
                LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, LOCATION_REQUEST, this);
                       }
    }

    /**
     * @param i
     */
    @Override
    public void onConnectionSuspended(int i) {
        Toast.makeText(getApplicationContext(), "location connection disconnected", Toast.LENGTH_LONG).show();
        LocationServices.FusedLocationApi.removeLocationUpdates(this.googleApiClient, this);
        isGoogleClientConnected = false;
    }

    /**
     * @param connectionResult
     */
    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        android.util.Log.e(Global.TAG, String.valueOf(connectionResult.getErrorCode()));
        isGoogleClientConnected = false;
        // try to reconnect is fails in infinite loop
        this.googleApiClient.reconnect();
    }

public void onLocationChanged(Location location) {
        Toast.makeText(getApplicationContext(), "onLocationChanged is called", Toast.LENGTH_LONG).show();

    }

1 个答案:

答案 0 :(得分:0)

尝试删除setExpirationTime

目前您的请求是

.setFastestInterval(1000).setExpirationTime(1000)

你要求在1000ms通过之前不回电,而不是在1000ms之后回调