gpt4 book ai didi

android - requestLocationUpdates 在位置变化实际上增加了一次时调用了很多次

转载 作者:行者123 更新时间:2023-11-30 03:51:32 27 4
gpt4 key购买 nike

我正在发送纬度、经度和位置名称以保存在服务器上....问题是当我第一次运行我的应用程序时它正常并保存以上数据一次...当我关闭我的应用程序并且模拟器正在运行时并且 eclipse 也再次启动我的应用程序然后它保存以上数据 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 的 location.java 类中,并在 onlocationchanged 中调用上面的函数

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);


}

最佳答案

mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mylocation);

如果您想确保 1000 毫秒已经过去并且客户端在位置更新之间移动了 5 米,则应该是以下内容

LocationListener customLocationListener = new CustomLocationListener();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 5, customLocationListener);

此外,在您的 Activity 中调用 onPause() 方法

mlocManager .removeUpdates(customLocationListener);

您必须像下面这样实现 CustomLocationListener:

private class CustomeLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
// when location changes, add the record in the database, send to server etc...
double lat = loc.getLatitude();
double lon = loc.getLongitude();
}

@Override
public void onProviderDisabled(String provider)
{}

@Override
public void onProviderEnabled(String provider)
{}

@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{}
}

关于android - requestLocationUpdates 在位置变化实际上增加了一次时调用了很多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14080431/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com