Search code examples
javaandroidgoogle-api-client

GoogleApiClient OnConnected Not Called unless Service is stopped


I am calling a service which connects to googleapiclient. Whenever the service is called first time, everything works smooth, client is connected, Onconnected is called. But the moment I recall service without stopping, I get googleapiclient connected but Onconnected is never called. Why So? Is there required to terminate service everytime. Here is the code:

public class ALW extends Service implements GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks, LocationListener {
    private GoogleApiClient mGoogleApiClient;



    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mGoogleApiClient.isConnected()) {
            mGoogleApiClient.disconnect();
        }
        Log.d("ALWFA", "Stopped");
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("ALWFA", "Called");
        if (!mGoogleApiClient.isConnected()) {
            Log.e("ALWFA", "Called for Connection");
            mGoogleApiClient.connect();
        } else {
            Log.e("ALWFA", "Already Connected");
        }
        //Do Work
        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    @Override
    public void onCreate() {
        super.onCreate();
        buildGoogleApiClient();
    }


    @Override
    public void onConnected(@Nullable Bundle bundle) {
        Log.d("Google Client", "Connected");
        //Do work

    }

    @Override
    public void onConnectionSuspended(int i) {
        System.out.println("Connection Sus");
        buildGoogleApiClient();
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        Log.i("Connection Error", "onConnectionFailed:" + connectionResult.getErrorCode() + "," + connectionResult.getErrorMessage());
        buildGoogleApiClient();
        System.out.println("Connection Failed");
    }

    synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }


}

and I m not getting any error in Onconenctiofailed. Normally code stuck at onStartCommand: "ALWFA Called". whenever service is called second time.


Solution

  • You should check before calling it again like this

       if(mGoogleApiClient!=null && !mGoogleApiClient.isConnected())
          {
            //Do your work
          }
    

    and implement these methods

      @Override
    protected void onStart() {
        super.onStart();
        if (mGoogleApiClient != null)
            mGoogleApiClient.connect();
    }
    
    @Override
    protected void onStop() {
        super.onStop();
        if (mGoogleApiClient != null)
            mGoogleApiClient.disconnect();
    
    }