locationmanager.requestForLocation is not working in motorola backflip & samsung galaxy

萝らか妹 提交于 2019-12-12 02:12:05

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!