When using Location data, is it better to make a new LocationClient in each activity, or is there a way to maintain the same one throughout?

StackOverflow https://stackoverflow.com/questions/20986870

سؤال

I will need to use Location data in multiple activities of my app and I was wondering if it was necessary to make a new LocationClient in each activity, or if there is a better way to create it once and access it from each activity. Any recommendations on how I should handle this?

هل كانت مفيدة؟

المحلول

I think it would be much better to create a single class for location listener and used it every where in your project.I have just created this in one of my package and in every class where i want to get the location values.I just call this.

LocationSender loc = new LocationSender(YourClass.this);

As i declared lat,lng as static so i can get any where its values.

double latitude = LocationSender.lat;
double longitude = LocationSender.lng;

LocationSender.java

public class LocationSender {
Context context;
LocationManager locationManager;
Location location;
static double lat,lng;
public LocationSender(Context ctx) {
    context=ctx;
    locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}

// Acquire a reference to the system Location Manager
public void getNewLocation(Location location) throws JSONException
{
    String latLongString = "";
    if (location != null) {
        this.location=location;
      lat= location.getLatitude();
       lng = location.getLongitude();
        latLongString = "Lat:" + String.valueOf(LocationActivity.lat) + "\nLong:" +  String.valueOf(LocationActivity.lng);
        Log.d("Location Found", latLongString);
       } else
    {
        location=null;
        latLongString = "No location found";
    }
    Toast.makeText(context, latLongString, Toast.LENGTH_LONG).show();
}






LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
      // Called when a new location is found by the network location provider.
        try {
            getNewLocation(location);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }



    public void onProviderEnabled(String provider) {

        Toast.makeText(context, "Provider: "+provider+" : Enabled", Toast.LENGTH_LONG).show();
    }

    public void onProviderDisabled(String provider) {

        Toast.makeText(context, "Provider: "+provider+" : disabled", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        Toast.makeText(context, "Provider: "+provider+" : status: "+status, Toast.LENGTH_LONG).show();

    }
  };    

 }

Also please dont forget to insert location permissions in AndroidManifest.xml :)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top