我正在构建一个Android应用程序,我正在使用Geocoder解码位置,但我注意到有时当连接相对较低时Gecoder不起作用。但在同一时间,浏览器和其他基于互联网的应用程序功能。有没有什么办法解决这一问题 ?这可能会出错。我确实理解它是一个后端服务,但它应该没关系。对于常规的http请求我们可以重试并修复类似的问题,但我该如何解决这个问题呢?有什么建议吗?
class ReverseGeocodingTask extends AsyncTask<Double, Void, String> implements OnDismissListener{
Context context;
ProgressDialog progDialog;
String progressString;
public ReverseGeocodingTask(Context context, String progressStr) {
this.context = context;
this.progressString = progressStr;
if(!isCancelled()){
initProgDialog();
}
}
void initProgDialog(){
progDialog = new ProgressDialog(context);
progDialog.setCanceledOnTouchOutside(false);
progDialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.btn_cancel),new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
progDialog.dismiss();
}
});
progDialog.setMessage(progressString);
progDialog.setOnDismissListener(this);
progDialog.show();
}
@Override
protected void onPreExecute() {
//removeAllPendingRevGeocodingTasks();
addTask(this);
super.onPreExecute();
}
@Override
protected String doInBackground(Double... params) {
String result = null;
if(!isCancelled()){
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
try {
List<Address> list = geocoder.getFromLocation(latitude, longitude, 1);
if (list != null && list.size() > 0) {
Address address = list.get(0);
result = address.getAddressLine(0) + ", " + address.getLocality();
}
} catch (IOException e) {
progDialog.dismiss();
failedToUpdateLocation();
Log.e("Location listener", "Failed to decode location name");
}
}
return result;
}
@Override
protected void onCancelled() {
progDialog.dismiss();
this.cancel(true);
super.onCancelled();
}
@Override
protected void onPostExecute(String result) {
latLongInDegrees = " "+latLongInDegreesToDMSString(latitude,longitude);
setLocationName(result);
updateLocationName(result + latLongInDegrees);
String progressStr=getString(R.string.msg_slp_fetch_from_geocoder_result_1)+latLongInDegreesToDMSString(latitude,longitude)+"...";
executeSLPWebserviceTask(latitude, longitude, progressStr);
finishedUpdatingLocation(latitude,longitude);
progDialog.dismiss();
super.onPostExecute(result);
this.cancel(true);
//removeAllPendingRevGeocodingTasks();
}
public void onDismiss(DialogInterface dialog) {
this.cancel(true);
}
}
protected void executeRevGeocoderTask(double lat, double longitude)
{
String progressStr=getString(R.string.msg_rev_geocoder_fetch)+" "+latLongInDegreesToDMSString(lat,longitude);
ReverseGeocodingTask task = new ReverseGeocodingTask(this,progressStr);
task.execute(lat,longitude);
}
答案 0 :(得分:2)
Geocoder是一种后端服务,我们对其http请求或任何类型的任何内容都没有任何处理。它需要良好的互联网连接。
答案 1 :(得分:0)
有时,Geocoder需要重新启动设备才能正常工作。它发生在Android 4上:我不知道如何解决这个问题。