Android 当位置更改实际增加一次时,多次调用RequestLocationUpdate
我正在发送纬度、经度和位置名称以保存在服务器上。。。。问题是,当我第一次运行我的应用程序时,它是ok的,并将上述数据保存一次。。。当我关闭我的应用程序,emulator正在运行,eclipse也在运行,并再次启动我的应用程序时,它会将上述数据保存2次。。。。。同样,当我关闭并再次启动我的应用程序时,它会增加1。。。我不知道它为什么多次调用Android 当位置更改实际增加一次时,多次调用RequestLocationUpdate,android,Android,我正在发送纬度、经度和位置名称以保存在服务器上。。。。问题是,当我第一次运行我的应用程序时,它是ok的,并将上述数据保存一次。。。当我关闭我的应用程序,emulator正在运行,eclipse也在运行,并再次启动我的应用程序时,它会将上述数据保存2次。。。。。同样,当我关闭并再次启动我的应用程序时,它会增加1。。。我不知道它为什么多次调用requestLocationUpdates()。。。无需发送到服务器,即可正常工作。。对此有什么想法请帮助我 正在将数据发送到服务器。。此函数在send.ja
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;
}
如果要确保已过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)
{}
}
如果要确保已过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)
{}
}
谢谢你的回答@hrehman。。。我已经在使用CustomLocationListener了,就像你在上面告诉我的那样,类名是Location。。。我在onpause()中调用了该方法“mlocManager.removeUpdates(customLocationListener);”但当我通过eclipse或telnet更改位置时,它会给出错误'threadid=1:thread带未捕获异常退出(group=0x40a13300)E/AndroidRuntime(1466):java.lang.RuntimeException:无法暂停活动{/com.qau..MainActivity}:java.lang.NullPointerException在android.app.ActivityThread.performPauseActivity(ActivityThread.java:2838)“我在你发布的代码中看不到你的LocationListener。请给我看一下。这是第二个块。我只给你看我自己的myLocationListener类中的onLocationchanged方法。。无论如何,谢谢你的回复,我在我的activity onDestroy()方法调用中更改了‘mlocManager.removeUpdates(customLocationListener)“有一个条件,它就运行了……再次感谢您的帮助……感谢您的回答@hrehman……我已经在使用CustomLocationListener,就像您在上面告诉我的那样,我的类名就是Location……我在onpause()中调用了该方法mlocManager.removeUpdates(CustomLocationListener)'但是当我通过eclipse或telnet更改位置时,会出现错误'threadid=1:thread带未捕获异常退出(group=0x40a13300)E/AndroidRuntime(1466):java.lang.RuntimeException:无法暂停活动{/com.qau..MainActivity}:java.lang.NullPointerException在android.app.ActivityThread.performPauseActivity上(ActivityThread.java:2838)“我在你发布的代码中看不到你的LocationListener。请给我看一下。这是第二个块。我只给你看我自己的myLocationListener类中的onLocationchanged方法。。无论如何,谢谢你的回复,我在我的activity onDestroy()方法调用中更改了‘mlocManager.removeUpdates(customLocationListener)“有一个条件,它就可以运行了……再次感谢你的帮助。。。。。