using Gps Getting Latitude and Longitude

96 views Asked by At

please anyone give me the code for, { getting latitude and longitude using gps for every 5 minutes, and send that latitude and longitude to Web url using Json Parsing

if you close that app also we able to get latitude and longitude

}

2

There are 2 answers

4
Ritvik Vyas On BEST ANSWER

http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/

Go to this link to get your location

You can implement a loop with postdelay handler to implement 5 minute refresh rate and run the getLocation inside that...

Use SharedPreferenceManager to store the last known lat and long to the sdcard data

https://mongskiewl.wordpress.com/2013/10/16/sending-json-data-from-android-to-a-php-script/

Follow this to send JSON data :)

0
Uma Kanth On

If you want to make your code run even if your application is not open, you need services for that.

A service runs in the background, If you want the latitude and longitude details you will need a LocationListener for that.

If you need your application to send details every 3 minutes, you will need to set a timer and get the lat and long and then send it using a JSON.

How to Create A Service

The Manifest File

<service
  android:name="MyService"
  android:icon="@drawable/icon"
  android:label="@string/service_name"
  >
</service> 

The Service java File

    public class MyService extends Service {

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    //TODO do something useful
      //write your repetition code here.
    return Service.START_NOT_STICKY;
  }

  @Override
  public IBinder onBind(Intent intent) {
  //TODO for communication return IBinder implementation
    return null;
  }
} 

Invoke your Service

Intent i= new Intent(context, MyService.class);
// potentially add data to the intent
i.putExtra("KEY1", "Value to be used by the service");
context.startService(i); 

Get Latitude and Longitude

  @Override
  public void onLocationChanged(Location location) {
    int lat = (int) (location.getLatitude());
    int lng = (int) (location.getLongitude());
    latituteField.setText(String.valueOf(lat));
    longitudeField.setText(String.valueOf(lng));
  }

So, what I suggest you is to run a background service which can take the details of the latitude and longitude periodically and then send it using a JSON.