GPS 位置未在服务器 onLocationChange 中更新

GPS location is not updating in server onLocationChange

在 GPS 管理器中,我在日志中获取每分钟的位置更新。

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,60000,0, this);

这一行成功了。

日志:

02-03 11:32:33.045    6217-6217/com.example.mylocation E/onLocationChanged﹕ 17.3732643
02-03 11:32:33.045    6217-6217/com.example.mylocation E/onLocationChanged﹕ 78.5046052
02-03 11:33:33.245    6217-6217/com.example.mylocation E/onLocationChanged﹕ 17.3732721
02-03 11:33:33.245    6217-6217/com.example.mylocation E/onLocationChanged﹕ 78.5046024
02-03 11:34:33.315    6217-6217/com.example.mylocation E/onLocationChanged﹕ 17.3732627
02-03 11:34:33.315    6217-6217/com.example.mylocation E/onLocationChanged﹕ 78.5046052
02-03 11:35:33.215    6217-6217/com.example.mylocation E/onLocationChanged﹕ 17.3732669
02-03 11:35:33.215    6217-6217/com.example.mylocation E/onLocationChanged﹕ 78.5046055
02-03 11:36:33.145    6217-6217/com.example.mylocation E/onLocationChanged﹕ 17.3732575
02-03 11:36:33.145    6217-6217/com.example.mylocation E/onLocationChanged﹕ 78.504606

如何在我的数据库中更新此信息。

此服务在单击按钮时启动,因此我实现了 Asynctask,其中 http 调用将数据发送到数据库

这只会发生一次,数据只会在数据库中输入一次,即在单击按钮时,每当调用 onLocationChanged() 时更新的数据不会发送到数据库

public void onLocationChanged(Location location)
{
    latitude = location.getLatitude();
    longitude = location.getLongitude();

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 0, this);

    Log.e("onLocationChanged",Double.toString(latitude));
    Log.e("onLocationChanged", Double.toString(longitude));

}

请帮忙解决这个问题

您的服务从点击按钮开始,正如您在代码中提到的那样,每隔一分钟接收一次位置更新,但是异步任务也是从点击按钮开始并将数据发送到服务器,它完成工作并结束,它不会再次启动,因此要将每次更新的数据发送到您的服务器,请在您的 onLocationChanged(Location location) 方法中实现异步任务

public void onLocationChanged(Location location)
{
    latitude = location.getLatitude();
    longitude = location.getLongitude();
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,60000,0, this);

    Log.e("onLocationChanged",Double.toString(latitude));
    Log.e("onLocationChanged", Double.toString(longitude));
    new yourAsynctask(parameters).execute();

}

@kevz 没有你的帮助是不可能的...非常感谢..

答案如下:

步骤 1::move AsyncTask 到 GpsTracker class

步骤2::startMainActivity

中的服务
Intent intent = new Intent(MainActivity.this, GPSTracker.class);
startService(intent);

步骤 3:: 在 GPSTracker.java

中使用 onStartCommand
  @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        getLocation();
        return START_STICKY;
    }

它并不像这里看起来那么简单..终于...完成了..