1

我正在发送纬度,经度和位置名称以保存在服务器上....问题是当我第一次运行我的应用程序时它可以并保存一次以上数据...当我关闭我的应用程序并且模拟器正在运行并且日食也并再次启动我的应用程序,然后将上述数据保存 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);


    }
4

1 回答 1

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

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

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

此外,在您的活动中 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)
    {}
}
于 2012-12-29T10:18:24.010 回答