我的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();
}
在这里,gast_enabled的吐司功能不会出现,但onLocationChanged不会出现。任何人都可以帮助我纠正这些问题。
也许问题不在这部分代码中。
首先,您无法在模拟器上模拟位置更改。您必须在真实设备(最好是室外设备)上进行测试。
要使用GPS获取正确的位置,通常您有时需要等待10-60秒(或更长时间)。这意味着,如果您使用后台服务(如我对AlarmManager所做的那样)来获取GPS位置,则必须等待一段时间才能将其关闭。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句