0

My app is heavily run off GeoCoordinates, and I have it so when users Post a Book it records the users geo coordinate like so:

PostBookButton.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {

                locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
                locationListener = new ObtainLocation();
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

            }
        });

public class ObtainLocation implements LocationListener{
        @Override
      public void onLocationChanged(Location loc) {


            if (loc != null)
            {
                Latitude = loc.getLatitude();
                Longitude = loc.getLongitude();


                ParseGeoPoint itemLoc = new ParseGeoPoint(Latitude,Longitude);

                    book.setGeoLocation(itemLoc);

                    book.saveInBackground(new SaveCallback()
                    {

                        @Override
                        public void done(ParseException e) {
                            if (e == null) 
                            {
                                finish();
                            } 
                            else 
                            {

                            }
                        }
                    });
                }
            }
            else
            {

            }

            locationManager.removeUpdates(locationListener);
      }

But problem is app will crash if the activity is ran with no network connection, seeing how it won't be able to obtain the geopoint coordinates.

How would I just make it so if the app fails on obtaining coordinates, instead of crashing maybe alert the user they cannot view books in their area due to no way to obtain their current location.

user3117785
  • 237
  • 5
  • 15
  • Have you tried leveraging Google Play Services Location APIs? They handle a lot of the grunt work for you to obtain locations. http://developer.android.com/google/play-services/location.html – WindsurferOak Mar 27 '14 at 17:31

2 Answers2

1

Update: The solution below is a general network connectivity one and won't necessarily tell you if you can get location updates through wifi as pointed out in comments.

You can register to receive network connectivity updates using a BroadcastReceiver. Register to listen for the android.net.conn.CONNECTIVITY_CHANGE action. Take a look at this for more info. Below, is a sample receiver.

public class ConnectivityReceiver extends WakefulBroadcastReceiver {
private static final String TAG = ConnectivityReceiver.class.getSimpleName();

@Override
public void onReceive(Context context, Intent intent) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected()) {
        Log.d(TAG, "network state change; new state: connected");
    } else {
        Log.d(TAG, "network state change; new state: " + (cm.getActiveNetworkInfo() != null ? cm.getActiveNetworkInfo().getState() : " unknown"));
    }
}

}

WindsurferOak
  • 4,681
  • 1
  • 27
  • 36
1

In addition to checking network accessibility, check if network location provider is enabled by user's system preferences:

ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
if ( lm == null || cm == null
     || !lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER) 
     || !cm.getActiveNetworkInfo().isConnected()) {
  // show alert: geolocation through network is inaccessible
}
Alex Salauyou
  • 13,188
  • 4
  • 39
  • 67