2

I am getting current location using Location Services api. Actually, I have verified whether location is enabled or not using Location Manager class in onResume() method and if is not enable then sending user to Location Setting using intent. After enabling location coming back to activity and trying to connect Google client api. But it is not connecting and not giving any location. I have used this code.

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

@Override
protected void onResume() {
    super.onResume();
    if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
            !manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle("Location is disabled")
                .setMessage("Please enable your location")
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                    }
                });

        AlertDialog dialog = builder.create();
        dialog.show();

    } else {
        Log.v("Connection Status", String.valueOf(mGoogleApiClient.isConnected()));
        mGoogleApiClient.connect();
    }
}

@Override
protected void onPause() {
    super.onPause();
    if (mGoogleApiClient.isConnected()){
        mGoogleApiClient.disconnect();
    }
}

GoogleApiClient.ConnectionCallbacks connectionCallbackListener = new GoogleApiClient.ConnectionCallbacks() {

    @Override
    public void onConnected(Bundle bundle) {
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

        if (mLastLocation != null){
            Log.v("Latitude", String.valueOf(mLastLocation.getLatitude()));
            Log.v("LOngitude", String.valueOf(mLastLocation.getLongitude()));
            startIntentService();
        }
    }
    @Override
    public void onConnectionSuspended(int i) {

    }
};

GoogleApiClient.OnConnectionFailedListener connectionFailedListener = new GoogleApiClient.OnConnectionFailedListener() {
    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Log.i("Connection failed", String.valueOf(connectionResult.getErrorCode()));
    }
};

And I am calling buildGoogleApiClient() method in onCreate() callback. Please someone tell me solution as it is very important.

aquib
  • 194
  • 2
  • 13
  • What does your log tell you? – Stephan Branczyk Jun 02 '15 at 05:37
  • @Stephan Branczyk The situation is that if my location is already enabled then I am getting current location in my log cat. But if location is not already enabled then I move to location settings and enable it. then move back to activity. But client api is not connecting and not getting current location in log cat. – aquib Jun 02 '15 at 06:10

1 Answers1

3

You could use startActivityForResult(), and then in onActivityResult() check to see if one of the location providers were enabled from the dialog prompt.

If either GPS or Network Location providers were enabled, then call mGoogleApiClient.connect().

Also note that you can use && instead of || in the initial check, since there is really no need to prompt the user with "Location is disabled" if at least one location provider is enabled.

@Override
protected void onResume() {
    super.onResume();
    if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER) &&
            !manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle("Location is disabled")
                .setMessage("Please enable your location")
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        startActivityForResult(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS), 100);
                    }
                });

        AlertDialog dialog = builder.create();
        dialog.show();

    } else {
        Log.v("Connection Status", String.valueOf(mGoogleApiClient.isConnected()));
        mGoogleApiClient.connect();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 100) {
        if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
                manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {

            //At least one provider enabled, connect GoogleApiClient
            mGoogleApiClient.connect();

        }
    }
}
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
  • @MohdAquib I just tested this on my device, and realized that it was returning `RESULT_CANCELED` for the `resultCode` when I clicked the back button from settings. So, I modified the answer, you can just take out the `resultCode` check since it's not really needed in this case. – Daniel Nugent Jun 02 '15 at 06:50
  • I have also get result code in log cat as 0. Now I modified my code..Thanks again – aquib Jun 02 '15 at 07:06
  • @MohdAquib No problem! – Daniel Nugent Jun 02 '15 at 07:09