How to periodically search for gps location in Android

I am writing an application that records a new gps location every ten minutes on a point.

I tried location counting, put the location listener in the service and put the location listener in action, but none of these methods work for me.

I need a way to turn on the GPS location receiver 2 minutes before I start recording location data (to give GPS enough time to find a location or determine the absence of a signal). I do this using an alarm clock that calls my gpsActivity 2 minutes before I call my updatelocation class. My updatelocation class then takes the gpsActivity locationmanager and extracts the location from it.

In theory, this should work, but when I put it on my phone, I always get bad data (accuracy is too low or not at all)

If anyone can help me, I would be very grateful.

thanks

An alarm triggers this class 2 minutes before the update.

public class GPSActivity extends Activity { public static LocationListener loc_listener = null; public static LocationManager locationManager = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); getGPS(); } public static void getGPS() { if (loc_listener == null) { loc_listener = new LocationListener() { @Override public void onStatusChanged(String provider, int status, Bundle extras) {} @Override public void onProviderEnabled(String provider) {} @Override public void onProviderDisabled(String provider) {} @Override public void onLocationChanged(Location location) {} }; } locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, loc_listener); } public static void killGPS() { if (locationManager != null && loc_listener != null) { locationManager.removeUpdates(loc_listener); } } } 

then in two minutes this service is called

 public class UpdateLocation extends IntentService { public static final String id = ""; public static int retryCount = 0; int notificationID = 1; // gets the location manager from the GPSActivity LocationManager locationManager = GPSActivity.locationManager; @Override public void onDestroy() { super.onDestroy(); } public UpdateLocation() { super("UpdateLocation"); } @Override protected void onHandleIntent(Intent intent) { locationManager = GPSActivity.locationManager; SharedPreferences prefs = getSharedPreferences("Settings", 0); final String id = prefs.getString("ID", ""); HttpParams httpParams = new BasicHttpParams(); // 30seconds and it stops HttpConnectionParams.setConnectionTimeout(httpParams, 30000); HttpConnectionParams.setSoTimeout(httpParams, 30000); DefaultHttpClient httpclient = new DefaultHttpClient(httpParams); HttpPost httpost = new HttpPost( "http://iphone-radar.com/gps/gps_locations"); JSONObject holder = new JSONObject(); try { holder.put("id", id); Location location = getLocation(); if (location != null && (location.getAccuracy() < 25)) { retryCount = 0; Calendar c = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat( "hh:mmaa MM/dd/yyyy"); holder.put("time", sdf.format(c.getTime())); holder.put("time_since_epoch", System.currentTimeMillis() / 1000); holder.put("lat", location.getLatitude()); holder.put("lon", location.getLongitude()); StringEntity se = new StringEntity(holder.toString()); httpost.setEntity(se); httpost.setHeader("Accept", "application/json"); httpost.setHeader("Content-type", "application/json"); ResponseHandler responseHandler = new BasicResponseHandler(); String response = httpclient.execute(httpost, responseHandler); org.json.JSONObject obj; obj = new org.json.JSONObject(response); SimpleDateFormat sdf2 = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ssaa"); addHistory( sdf2.format(c.getTime()), "Background GPS", "Latitude: " + String.format("%.6g%n", location.getLatitude()) + "\n" + "Longitude: " + String.format("%.6g%n", location.getLongitude())); SharedPreferences.Editor editor = prefs.edit(); editor.putString("LastUpdatedTime", sdf.format(c.getTime())); editor.commit(); Intent setAlarm = new Intent(UpdateLocation.this, UpdateLocation.class); PendingIntent pendingIntent = PendingIntent.getService( UpdateLocation.this, 0, setAlarm, 0); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); int UPDATE_TIME = prefs.getInt("Update_time", 10); calendar.add(Calendar.MINUTE, UPDATE_TIME); alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); // sets an alarm for the next time we need to record a // location GPSActivity.killGPS(); // turns off the gps location listener Intent setAlarm2 = new Intent(UpdateLocation.this, GPSActivity.class); PendingIntent pendingIntent2 = PendingIntent.getService( UpdateLocation.this, 0, setAlarm2, 0); Calendar calendar2 = Calendar.getInstance(); calendar2.add(Calendar.MINUTE, UPDATE_TIME - 2); alarmManager.set(AlarmManager.RTC_WAKEUP, calendar2.getTimeInMillis(), pendingIntent2); // sets an alarm to turn on the gpsActivity 2mins before the // next time updatelocation needs to record some data } } finally { } } } 
+8
android gps
source share
1 answer

Use the receiver and service together. To do this, you can find the full sample in this link . It has a listener. A listener can be used in your work to be noticed that a new place is ready for you.

+4
source share

All Articles