我正在发送纬度,经度和位置名称以保存在服务器上....问题是当我第一次运行我的应用程序时它可以并保存一次以上数据...当我关闭我的应用程序并且模拟器正在运行并且日食也并再次启动我的应用程序,然后将上述数据保存 2 次.....同样,当我关闭并重新启动我的应用程序时,它增加 1...我不知道为什么它会requestLocationUpdates()
多次调用...没有发送到服务器它工作正常..有关此的任何想法请帮助我...
向服务器发送数据.. 这个函数在 send.java 类中实现
public String postData(List<? extends NameValuePair> nameValuePairs, String wurl){
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(wurl);
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
InputStream is = response.getEntity().getContent();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(20);
int current = 0;
while((current = bis.read()) != -1){
baf.append((byte)current);
}
/* Convert the Bytes read to a String. */
text = new String(baf.toByteArray());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
return text;
}
onlocationchaned 函数位于实现 LocationListener 并在 onlocationchanged 中调用上述函数的 location.java 类中
public void onLocationChanged(android.location.Location loc) {
String response;
latitude = loc.getLatitude();
longitude = loc.getLongitude();
// Changing type double to string for sending
lati = Double.toString(latitude);
logi = Double.toString(longitude);
String Text = "My current location is: " +
" Latitude = " + Location.lati +
" Longitude = "+ Location.logi;
Toast.makeText(c, Text , Toast.LENGTH_LONG).show();
Log.d("lat", "Changed");
locationName = getAddress(latitude, longitude);
// Add location related data
List<BasicNameValuePair> locationNameValuePair = new ArrayList<BasicNameValuePair>(4);
locationNameValuePair.add(new BasicNameValuePair("latitude", lati));
locationNameValuePair.add(new BasicNameValuePair("longitude", logi));
locationNameValuePair.add(new BasicNameValuePair("locName", locationName));
response = con.postData(locationNameValuePair, wurl);
Toast.makeText(c, response, Toast.LENGTH_LONG).show();
Log.v("IGA", "Address " + locationName);
Toast.makeText(c, locationName, Toast.LENGTH_LONG).show();
}
public String getAddress(double lat, double lng) {
Geocoder geocoder = new Geocoder(c, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
Address obj = addresses.get(0);
//String add = obj.getAddressLine(0);
String add = obj.getThoroughfare();
add = add + ", " + obj.getSubLocality();
add = add + ", " + obj.getLocality();
add = add + ", " + obj.getCountryName();
return add;
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(c, e.getMessage(), Toast.LENGTH_LONG).show();
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
Toast.makeText(c, e.getMessage(), Toast.LENGTH_LONG).show();
}
return "";
}
和扩展 MapActivity 的 mainavtivity 中的 locationManager
protected void onCreate(Bundle savedInstanceState) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MapView view = (MapView) findViewById(R.id.themap);
view.setBuiltInZoomControls(true);
LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location mylocation = new Location(MainActivity.this);
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mylocation);
}