You are using
getLastKnownLocation()
to get location but You doesn't uselocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000, 0, null);
The second parameter is delation between updates in milliseconds. The fourth parameter is listener, you can use it to update locations vithout any async threads, for example:
...
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000, 0, this);
...
public void onLocationChanged(Location arg0) {
if (arg0 != null){
double longitude = arg0.getLongitude();
double latitude = arg0.getLatitude();
...
}
}
LocationManager.NETWORK_PROVIDER
is no good way to recieve location because it using location recieved by another location services when device was near wifi router. If location was not recieved near some router by another services in some reasons it will not provide you correct location.There is a bug in some devices (for example Sony) when mobile data turned on and networkInfo == null. Some times it solves getting needed networkInfo directly. For Exmple:
android.net.NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI); android.net.NetworkInfo mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
Example:
public class MyActivity extends Activity implements LocationListener
{
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
... // do something
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000, // every 2 seconds
0, this);
}
...
public void onLocationChanged(Location arg0) {
if (arg0 != null){
double longitude = arg0.getLongitude();
double latitude = arg0.getLatitude();
...
}
}
}