using Gps Getting Latitude and Longitude

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.

Leave a Comment