问题
I am newbie in Android, I am trying to retrieve the GPS data in my application.
I am using following code
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)){
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this);
}
And my class I have overriden the following method
public void onLocationChanged(Location location)
{
String currentLocation = "The location is changed to Lat: " + location.getLatitude()+ " Lng: " + location.getLongitude();
Toast.makeText(context, currentLocation, Toast.LENGTH_LONG);
}
But it is not showing anything and this is happening only with motorola backflip & samsung galaxy devices. Please let me know if I am doing anything wrong. Thanks in advance.
Thansk, Devanand
回答1:
You forgot to use Toast.makeText(...).show();
回答2:
Are you sure this code shows something for other phones? As Neeraj answered, you have to add .show() to the Toast.
If that is not an issue & you just missed it here, then you might have come across a problem I was facing too. A piece of code similar to yours worked perfectly on Motorola Atrix, but not on Motorola Droid.
Here was my workaround. In case GPS is not available, I fall back on the Network provider. Like so:
//Start with fine location using GPS
String locationProviderInit = LocationManager.GPS_PROVIDER;
if ((locationProviderInit == null) ||
!(mLocationManager.isProviderEnabled(locationProviderInit)))
{
//If GPS not available, fall back on coarse location
locationProviderInit = LocationManager.NETWORK_PROVIDER;
}
Hope this helps!
来源:https://stackoverflow.com/questions/6496588/locationmanager-requestforlocation-is-not-working-in-motorola-backflip-samsung