如何使用时间戳将 GPS 坐标和当前移动速度发送到 phone db

How to send GPS cordinates and current moving speed to phone db with timestamp

我是 Android 的新手。我尝试了几种方法将 GPS 坐标存储在我的 phone 数据库中。

现在的问题是我想每 x 秒存储一次 GPS 坐标以及当前移动的车辆速度和时间。

此外,此函数应在按钮单击事件后启动。

你只需要制作一个后台服务,它会每隔 x 秒连续获取 GPS 坐标。为此提供服务的主要原因是当您的应用程序不可见并且它被最小化时,您的服务将被保留 运行 并且在应用程序的后台 GPS 坐标将定期更新。

要实现,您需要 FusedLocationAPI 和 GooglAPIClient。如果你想要一个非常简单的代码而不是遵循这个 link: Android make method run every X seconds (involves gps and server call)

创建服务并启动计时器,如图所示below.Hope这有帮助

 public void starttimer(){
   timer = new CountDownTimer(300000, 2000) {
    @Override
    public void onTick(long millisUntilFinished) {
           locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String provider = locationManager.getBestProvider(criteria, true);
    try {
        location = locationManager.getLastKnownLocation(provider);
    }
    catch (Exception e)
    {

    }
    if(location==null) {
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    }

    if (location != null) {
        double lat = location.getLatitude();
        double longg = location.getLongitude();
        mMap.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(lat,longg) , 14.0f) );


    } else {
        Toast.makeText(MainActivity.this, "Device Failed To Fetch Lat Long", Toast.LENGTH_LONG).show();
    }
    }


    @Override
    public void onFinish() {
        //your code when timer finishes
    }
}.start();
  }