将 Android 协调的 GPS 发送到服务器

发布于 2024-12-05 16:22:32 字数 2288 浏览 1 评论 0原文

我希望我的后台服务在位置发生变化时将当前的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

路弥 2024-12-12 16:22:32

您永远不会向 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.

一人独醉 2024-12-12 16:22:32

我没有看到您开始请求位置更新的任何地方。您能否确认您正在拨打以下电话:

locationManager.requestLocationUpdates(provider, 0, 0, this);

I don't see anywhere that you start requesting location updates. Can you confirm you're calling the following:

locationManager.requestLocationUpdates(provider, 0, 0, this);
心碎的声音 2024-12-12 16:22:32

温馨提示:不要在每次位置发生变化时都发布位置,而是仅在新位置比前一个位置更好时才发布。 查看此页面以确定是否一个位置比另一个位置好。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文