-1

I'm new in to Android. I tried several ways to store GPS coordinates in my phone db.

Now the problem is I want to store GPS cordinates every x seconds with current moving vehicle speed and the time.

Also this function should start after a button click event.

halfer
  • 19,824
  • 17
  • 99
  • 186
H.Kasun
  • 3
  • 3
  • This post describes how to define the service and store the positions in the database [service](https://stackoverflow.com/a/44775200/4631177) – Alireza Jul 04 '17 at 13:08
  • Possible duplicate of [Run Gps as background service and Compare the received current Location Data with value in Sqlite?](https://stackoverflow.com/questions/39148214/run-gps-as-background-service-and-compare-the-received-current-location-data-wit) – Jedi Jul 05 '17 at 00:12

2 Answers2

1

You just need to make one background service which will continuously fetch the GPS coordinates in every x seconds. The main reason behind making the service for this is when your application is not visible and it is minimized than also your service will be kept running and in the background of the application the GPS coordinates will be updated periodically.

For implementing you would need a FusedLocationAPI and GooglAPiClient. If you want a very simple code than follow this link: Android make method run every X seconds (involves gps and server call)

halfer
  • 19,824
  • 17
  • 99
  • 186
Pranay Soni
  • 403
  • 3
  • 12
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Dwhitz Jul 04 '17 at 12:39
  • thank you @Dwhitz for your suggestion, i will surely take your suggestion into consideration while answering the next question. – Pranay Soni Jul 04 '17 at 13:25
  • thnx much appreciate your answer @Pranay Soni – H.Kasun Jul 04 '17 at 15:50
0

create a service and start a timer like shown below.Hope this helps

 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();
  }
shivadeep
  • 68
  • 1
  • 11