1

i want to send my location into my database.. i have code to get location (longitude and latitude) like this

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    LocationManager locationManager;
    String context = Context.LOCATION_SERVICE;
    locationManager = (LocationManager)getSystemService(context);

    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    String provider = locationManager.getBestProvider(criteria, true);

    //Location location = locationManager.getLastKnownLocation(provider);
    updateWithNewLocation(null);

    locationManager.requestLocationUpdates(provider, 2000, 10,
                                           locationListener);

}
private final LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
      updateWithNewLocation(location);
    }

    public void onProviderDisabled(String provider){
      updateWithNewLocation(null);
    }

    public void onProviderEnabled(String provider){ }
    public void onStatusChanged(String provider, int status, 
                                Bundle extras){ }
  };
  private void updateWithNewLocation(Location location) {
        //EditText etlat=(EditText)findViewById(R.id.lat);
        //EditText etlong=(EditText)findViewById(R.id.lng);


        if (location != null) {
            double lat = location.getLatitude();
            System.out.println(lat);
             double lng = location.getLongitude();
             System.out.println(lng);
             double alt = location.getAltitude();
             System.out.println(alt);
             etlat.setText(String.valueOf(lat));
             etlng.setText(String.valueOf(lng));
             etalt.setText(String.valueOf(alt));
        } 
  }

and then i want to save my location continues into database with interval 10 minute..
how can i do this??thank you :)

j0k
  • 22,600
  • 28
  • 79
  • 90
akubabas
  • 473
  • 5
  • 13
  • 28

3 Answers3

2
 locationManager.requestLocationUpdates(provider,(10*60*1000),10,
                                           locationListener);

You can try this.

j0k
  • 22,600
  • 28
  • 79
  • 90
Shashank Degloorkar
  • 3,151
  • 4
  • 32
  • 50
1

Write a class that extends Service and add the appropriate tag in your manifest file.

Write the code that you want to execute when service is running like fetching location and saving it in database in onCreate() method.

You can refer to this tutorial: http://www.vogella.com/articles/AndroidServices/article.html

shraddha
  • 309
  • 1
  • 2
  • 13
0

Just start the timer with interval of 10 minutes.. and the make requestLocationUpdate();

then u will get set of lat, log values for each GPS fix.. get best value from it by location.getAccuracy().. and write the code for database insertion.

NullPointerException
  • 3,978
  • 4
  • 34
  • 52