Search code examples
javaandroidlocationandroid-6.0-marshmallowandroid-gps

Simple code to get GPS coordinates in android marshmallow


i am developing an android app that targets marshmallow. i did enough searching but could not get a simple code to get GPS coordinates. i wanted a code that could give me GPS coordinates over some intervals. like for eg. every 10 mins i need to check the coordinates if person is in a specific area for duration of 30 mins. so app will check coordinates for every 10 mins that 3 times. Please help. :(

Note: i know about the gpstracker.java example available but it doesn't works for marshmallow. I want the GPS coordinates in form of longitude and latitude. Help with link is also appreciated.


Solution

  • There are 2 steps to solve your problem : 1) There should be something that triggers your code (Fetching GPS Coordinates code) every 10 minutes. 2) The actual code for fetching the GPS coordinates.

    Solutions: 1) There are 2 ways you can schedule your code to run every 10 minutes : Alarm Manager or Job Scheduler

    Alarm Manager can be used in lower end devices while Job scheduler require minimum api 21 Lollipop. Check out this tutorials for doing so :

    Alarm Manager Tutorial

    Job Scheduler Tutorial

    2) Now comes the part of actually fetching the gps coordinates. You can check out this article for that GPS Location programmatically