I would like to know there is a way to get location if only it changes? I know android provides this http://developer.android.com/training/location/receive-location-updates.html
// try this way
// declaration
public static LocationManager mlocManager;
public static LocationListner mListner;
private static String latitude;
public static String longitude;
//initialization
try {
if (mlocManager == null)
mlocManager = (LocationManager) getSystemService(Activity.LOCATION_SERVICE);
if (mListner == null)
mListner = new LocationListner();
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
try {
mlocManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 0, 0, mListner);
} catch (Throwable e) {
e.printStackTrace();
}
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mListner);
} catch (Throwable e) {
e.printStackTrace();
}
try {
mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mListner);
} catch (Throwable e) {
e.printStackTrace();
}
}
});
} catch (Throwable e) {
e.printStackTrace();
}
//listener
class LocationListner implements LocationListener {
@Override
public void onLocationChanged(Location location) {
setLatitude("" + location.getLatitude());
setLongitude("" + location.getLongitude());
setCurrentLocation(location);
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
// Setter
public void setLatitude(String latitide) {
this.latitude = latitide;
}
// Getter
public String getLatitude() {
try {
if (latitude != null) {
return latitude;
}
if(mlocManager==null)
{
mlocManager = (LocationManager) getSystemService(Activity.LOCATION_SERVICE);
}
Location loc = mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (loc == null) {
loc = mlocManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (loc == null) {
loc = mlocManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
}
if (loc != null) {
return "" + loc.getLatitude();
}
} else {
return "" + loc.getLatitude();
}
} catch (Exception e) {
e.printStackTrace();
}
return "0";
}
// Setter
public void setLongitude(String longitude) {
this.longitude = longitude;
}
// Getter
public String getLongitude() {
try {
if (longitude != null) {
return longitude;
}
if(mlocManager==null)
{
mlocManager = (LocationManager) getSystemService(Activity.LOCATION_SERVICE);
}
Location loc = mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (loc == null) {
loc = mlocManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (loc == null) {
loc = mlocManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
}
if (loc != null) {
return "" + loc.getLongitude();
}
} else {
return "" + loc.getLongitude();
}
} catch (Exception e) {
e.printStackTrace();
}
return "0";
}