【问题标题】:onLocationChanged(Location location) never get calledonLocationChanged(Location location) 永远不会被调用
【发布时间】:2014-09-18 06:42:42
【问题描述】:

我的 locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 返回 true 但在位置改变时永远不会被调用。我的 wifi 已打开,GPS 卫星和 wifi 以及网络位置已打开我无法解释为什么它没有被调用。我在下面附上了所需的代码。

try
    {
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        gps_enabled=locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);        
        network_enabled=locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if(!gps_enabled && !network_enabled)
            Toast.makeText(MainActivity.this, "Error Getting Your Location", Toast.LENGTH_LONG).show();
        else
        {
            if(network_enabled)
            {
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
                Toast.makeText(MainActivity.this, "network_enabled", Toast.LENGTH_LONG).show();
            }
            if(gps_enabled)
            {
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
                Toast.makeText(MainActivity.this, "gps_enabled", Toast.LENGTH_LONG).show();
            }
        }
    }
    catch(Exception ex){}


@Override
public void onLocationChanged(Location location) {  
    Toast.makeText(MainActivity.this, "onLocationChanged", Toast.LENGTH_LONG).show();
    this.lat = location.getLatitude();
    this.lon = location.getLongitude(); 
    (new CityList()).execute();
}

这里的 toast gps_enabled 但 onLocationChanged 从未出现。任何人请帮我纠正这些事情。

【问题讨论】:

  • onLocationChanged(....)之前添加@Override
  • @GG 我看不到的地方..首先正确发布您的代码...
  • 它在我的实际代码中。我这里没有贴。但现在我进行编辑。请参考

标签: android location locationmanager


【解决方案1】:

可能问题不在这部分代码中。

首先,你不能在模拟器上模拟位置变化。您必须在真实设备上进行测试,最好是在户外。

要使用 GPS 获得正确的位置,通常您有时需要等待 10-60 秒(或更长时间)。这意味着如果您使用后台服务(就像我使用 AlarmManager 所做的那样)来获取 GPS 位置,您必须等待一段时间才能关闭它。

【讨论】:

    猜你喜欢
    • 2023-03-18
    • 2012-02-18
    • 2017-01-20
    • 1970-01-01
    • 2013-07-22
    • 2013-01-02
    • 1970-01-01
    • 2012-08-19
    • 2013-03-22
    相关资源
    最近更新 更多