0

I seem to have a problem that I just can't figure out. I am creating an application that requires Location Services with Android. The application needs to constantly poll for location. This code had been working for months, and then when I uninstalled it from my phone and reinstalled it, the location is suddenly returning null.

Here is the LocationProvider class, which grabs the location using a Google API client:

public class LocationProvider implements
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener,
    LocationListener {

public abstract interface LocationCallback {
    public void handleNewLocation(Location location);
}

public static final String TAG = LocationProvider.class.getSimpleName();

/*
 * Define a request code to send to Google Play services
 * This code is returned in Activity.onActivityResult
 */
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;

private LocationCallback mLocationCallback;
private Context mContext;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;


public LocationProvider(Context context, LocationCallback callback) {
    mGoogleApiClient = new GoogleApiClient.Builder(context)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();

    mLocationCallback = callback;

    // Create the LocationRequest object
    mLocationRequest = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)

            .setInterval(3 * 1000)        // 10 seconds, in milliseconds
            .setFastestInterval(1 * 1000); // 1 second, in milliseconds

    mContext = context;
}

public void connect() {
    mGoogleApiClient.connect();
}

public void disconnect() {
    if (mGoogleApiClient.isConnected()) {
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        mGoogleApiClient.disconnect();
    }
}

@Override
public void onConnected(Bundle bundle) {
    Log.i(TAG, "Location services connected.");
    Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

    if (location == null) {
        //----------------

        //----------------
         Log.i(TAG, "couldnt get location");
        noLocationEnabledDialog();
        /*LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
        mLocationCallback.handleNewLocation(location); */
    }
    else {
        /* Log.i(TAG, "couldnt get location");
        noLocationEnabledDialog(); */
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
        mLocationCallback.handleNewLocation(location);
    }
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    /*
     * Google Play services can resolve some errors it detects.
     * If the error has a resolution, try sending an Intent to
     * start a Google Play services activity that can resolve
     * error.
     */
    if (connectionResult.hasResolution() && mContext instanceof Activity) {
        try {
            Activity activity = (Activity)mContext;
            // Start an Activity that tries to resolve the error
            connectionResult.startResolutionForResult(activity, CONNECTION_FAILURE_RESOLUTION_REQUEST);
        /*
         * Thrown if Google Play services canceled the original
         * PendingIntent
         */
        } catch (IntentSender.SendIntentException e) {
            // Log the error
            e.printStackTrace();
        }
    } else {
        /*
         * If no resolution is available, display a dialog to the
         * user with the error.
         */
        Log.i(TAG, "Location services connection failed with code " + connectionResult.getErrorCode());
    }
}

public void noLocationEnabledDialog(){
        AlertDialog.Builder setLocationDialog = new AlertDialog.Builder(mContext);
        setLocationDialog.setTitle(R.string.location_message_title);
        setLocationDialog.setMessage(R.string.location_message);

        /*Button takes user to settings*/
        setLocationDialog.setPositiveButton(R.string.affirmative, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                mContext.startActivity(settingsIntent);
            }
        });
        /*If no location, close the activity*/
        setLocationDialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //do nothing....yet
            }
        });
    setLocationDialog.show();
}

@Override
public void onLocationChanged(Location location) {
    mLocationCallback.handleNewLocation(location);
    Log.i(TAG, "New location received ");
} } 

I have also tried turning on Google Maps to grab a location on the device, hoping that it would use the location grabbed from Maps. Curiously, the location icon showed up in the top bar before reinstalling and it no longer shows. Does anyone know why reinstalling the application would cause this error? I am running this on a Samsung Galaxy s6 running Android 6.0.1

Thank you very much!

cloudyGirrl
  • 5
  • 1
  • 4

1 Answers1

0

Well, it appears I have answered my own question. I am a serious derp. Marshmallow requires that individual apps ask for permission for location and such. Since the previous installation had a different target API, it didnt need that and the manifest sufficed.

I brought the target API down to 22 and that was it. In the future, I will need to ask for permission. This question answers it quite well:

Permission issues for location in android Marshmallow applicaton

Community
  • 1
  • 1
cloudyGirrl
  • 5
  • 1
  • 4