将 Android 协调的 GPS 发送到服务器
我希望我的后台服务在位置发生变化时将当前的 GPS 坐标发送到服务器。现在我有这样的代码:
public void onStart(Intentintent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location loc) {
loc.getLatitude();
loc.getLongitude();
double lat = loc.getLatitude();
double lon = loc.getLongitude();
String curr_lat = Double.toString(lat);
String curr_lon = Double.toString(lon);
postData(curr_lat,curr_lon);
String Text = "My current location is: " +
"Latitud = " + loc.getLatitude() +
"Longitud = " + loc.getLongitude();
Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_LONG).show();
}
问题是当位置改变时,方法 postData 永远不会被调用。 postData 使用 HTTP 将当前 GPS 坐标发送到服务器
public void postData(String currLat, String currLon) {
// List with arameters and their values
String Text2 = "String is: " +
"Latitud = " + currLat +
"Longitud = " + currLon;
Toast.makeText(getApplicationContext(), Text2, Toast.LENGTH_LONG).show();
HttpPost post = new HttpPost("http://www.url.com/*.php");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("myusername", currLat));
nameValuePairs.add(new BasicNameValuePair("mypassword", currLon));
HttpClient client = new DefaultHttpClient();
try {
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
String responseText = EntityUtils.toString(entity);
responseText = responseText.trim();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
什么不是调用函数 postData?甚至它一次也没有叫过。
I want my background service to send current GPS coordinated to the server whenever location is changed. right now I have this code:
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location loc) {
loc.getLatitude();
loc.getLongitude();
double lat = loc.getLatitude();
double lon = loc.getLongitude();
String curr_lat = Double.toString(lat);
String curr_lon = Double.toString(lon);
postData(curr_lat,curr_lon);
String Text = "My current location is: " +
"Latitud = " + loc.getLatitude() +
"Longitud = " + loc.getLongitude();
Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_LONG).show();
}
The problem is that method postData is never called when location change. postData send current GPS coordinated to the server using HTTP
public void postData(String currLat, String currLon) {
// List with arameters and their values
String Text2 = "String is: " +
"Latitud = " + currLat +
"Longitud = " + currLon;
Toast.makeText(getApplicationContext(), Text2, Toast.LENGTH_LONG).show();
HttpPost post = new HttpPost("http://www.url.com/*.php");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("myusername", currLat));
nameValuePairs.add(new BasicNameValuePair("mypassword", currLon));
HttpClient client = new DefaultHttpClient();
try {
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
String responseText = EntityUtils.toString(entity);
responseText = responseText.trim();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
What it is not calling function postData? Even it did not call it for once.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您永远不会向 locationManager 注册您的 locationListener。您应该使用 requestLocationUpdates
不要忘记调用removeUpdates 与 onStop 中的侦听器您的服务。
You never register your locationListener with the locationManager. You should use one form of requestLocationUpdates
Don't forget to call removeUpdates with your listener in the onStop of your service.
我没有看到您开始请求位置更新的任何地方。您能否确认您正在拨打以下电话:
I don't see anywhere that you start requesting location updates. Can you confirm you're calling the following:
温馨提示:不要在每次位置发生变化时都发布位置,而是仅在新位置比前一个位置更好时才发布。 查看此页面以确定是否一个位置比另一个位置好。
Just a tip: rather than post the location every time it changes, only post it if the new location is better than the previous one. Check this page for a way to determine if one location is better than another.