GoogleApiClient 没有在服务内部调用 onConnected 方法

GoogleApiClient not calling onConnected method inside service

我正在尝试通过服务更新位置,但是当我调用 googleApiClient 进行连接时,它没有调用任何方法,例如 onConnected、onConnectionSuspended 或 onConnectionFailed。

完整代码如下:

public abstract class BASE_LOCAT_SERVICE extends Service  implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener, LocationListener {

    public String TAG="LOC_SERVICE";

    public GoogleApiClient googleApiClient;
    private Location lastLocation;
    private static int UPDATE_INTERVAL = 5000;
    private static int DISPLACEMENT = 10;
    private LocationRequest locationRequest;


    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG+"_LOC","On create Command");
        buildGoogleApiClient();
    }


    protected synchronized void buildGoogleApiClient() {
        googleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API).build();
        Log.i(TAG+"_LOC","googleApiClient Connecting");
    }

    protected void createLocationRequest() {
        locationRequest = new LocationRequest();
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationRequest.setInterval(UPDATE_INTERVAL);
        locationRequest.setSmallestDisplacement(DISPLACEMENT);
        Log.i(TAG+"_LOC","createLocationRequest");
    }


    private void initialiseWithLastLocation() {
        try{
            lastLocation = LocationServices.FusedLocationApi.getLastLocation(
                    googleApiClient);
            Log.i(TAG+"_LOC","initialiseWithLastLocation Initialised");
        }catch(SecurityException ex){
            Log.e(TAG+"_LOC", "Unable to get last known location", ex);
        }
    }

    protected void startLocationUpdates() {

        try {
            LocationServices.FusedLocationApi.requestLocationUpdates(
                    googleApiClient, locationRequest, this);
            Log.i(TAG+"_LOC","startLocationUpdates Initialised");
        } catch (SecurityException ex) {
            Log.e(TAG+"_LOC", "Unable to get Last Known location", ex);
        }
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        createLocationRequest();
        initialiseWithLastLocation();
        startLocationUpdates();
        Log.i(TAG+"_LOC","googleApiClient Connected");
    }

    @Override
    public void onConnectionSuspended(int i) {
        Log.i(TAG+"_LOC","googleApiClient onConnectionSuspended");
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        Log.i(TAG+"_LOC","googleApiClient onConnectionFailed");
    }

    @Override
    public void onLocationChanged(Location location) {
        lastLocation = location;
        Log.i(TAG+"_LOC"," onLocationChanged");
    }

    public String getLastLocation()
    {
        if(lastLocation!=null)
        {
            String ret="Location : \n";

            ret = ret+"Latitude : "+lastLocation.getLatitude()+"\n";
            ret = ret+"Longitude : "+lastLocation.getLongitude()+"\n";
            ret = ret+"Altitude : "+lastLocation.getAltitude()+"\n";
            ret = ret+"Accuracy : "+lastLocation.getAccuracy()+"\n";
            ret = ret+"Provider : "+lastLocation.getProvider()+"\n";
            ret = ret+"Speed : "+lastLocation.getSpeed()+"\n";
            ret = ret+"MAP Link : https://www.google.com/maps/?q="+lastLocation.getLatitude()+","+lastLocation.getLongitude()+"\n";

            return ret;
        }
        else
            return "Location Not Available";
    }
}

logcat 仅显示此内容:

LOC_SERVICE_LOC: On create Command
LOC_SERVICE_LOC: googleApiClient Connecting

您必须在 onStartCommand

中使用 mGoogleApiClient.connect() 进行连接
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (mGoogleApiClient != null) {
        mGoogleApiClient.connect();
    }else{
       Toast.makeText(getApplicationContext(),"mGoogleApiClient is null",Toast.LENGTH_LONG).show();
    }
    return START_STICKY;
}