这是我的程序的要点..当我按下按钮告诉位置时,如果地理编码器没有抛出任何异常,它工作正常,但问题出现时地理编码器抛出异常,同时我的应用程序停止了一段时间就像它挂起..因为进度条停止旋转......并且停止按钮也没有按下......但是在30秒或1分钟之后它没有打印出来的地址......任何人都可以帮我解决这个问题...提前谢谢你 .
//这是在onCreate方法中的locationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,3,this);
//分隔Mehtod public void geo(双纬度,双经度){
if(Geocoder.isPresent())
{
try
{
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
List<Address> listAddresses = geocoder.getFromLocation(latitude, longitude, 1);
if(null!=listAddresses&&listAddresses.size()>0)
{
Location = listAddresses.get(0).getAddressLine(0);
}
else
{
Toast.makeText(getBaseContext(), "Searching..",dur).show();
}
}
catch (Exception e)
{
//Toast.makeText(this,Location, dur).show();
Toast.makeText(getBaseContext(), "No address found",dur).show();
}
}
else
{
Toast.makeText(getApplicationContext(),"wait...",dur).show();
}
}
获取纬度和经度后,在获取位置时,使用onLocationChanged方法调用此geo方法 .
2 回答
我假设问题出在地理编码器's attempts to get your location (maybe it'只是等待来自传感器或网络的响应) . 您应该在单独的线程中对地理编码器执行请求以防止挂起(您可以使用AsyncTask) . 但请注意,所有使用UI的工作,如显示祝酒词,都必须在主UI线程中完成(您可以使用方法runOnUiThread from Activity)
考虑位置服务,您每1秒实例化一次收集位置信息,使用此服务不是在UI线程中而是在服务或线程中,这是一个很好的做法,并且遵循以下步骤是一个好习惯: