I want the location updates to be sent to the server every 15mins to time interval from android phone to the server.Is the service or the alarm manager is the best option.
If i start a service,can i start an asynctask to post locations to the server.?
here is the code i used :
@Override
public void onStart(Intent intent, int startId) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
listener = new MyLocationListener();
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 4000, 0, listener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
4000, 0, listener);
}
public class MyLocationListener implements LocationListener {
public void onLocationChanged(final Location loc) {
Log.i("**************************************", "Location changed");
if (isBetterLocation(loc, previousBestLocation)) {
loc.getLatitude();
loc.getLongitude();
//
// intent.putExtra("Latitude", loc.getLatitude());
// intent.putExtra("Longitude", loc.getLongitude());
// intent.putExtra("Provider", loc.getProvider());
//
// sendBroadcast(intent);
}
}
public void onProviderDisabled(String provider) {
Toast.makeText(getApplicationContext(), "Gps Disabled",
Toast.LENGTH_SHORT).show();
}
public void onProviderEnabled(String provider) {
Toast.makeText(getApplicationContext(), "Gps Enabled",
Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
}
I dont know how to run the service background every fifteen minutes of interval.. I want to start an aysnctask when i get fresh location updates.Is the service or the alarm manager is the best option. If i start a service,can i start an asynctask to post locations to the server.?
Yes you can schedule alarms for a receiver every 15 mins and trigger a service which can start an AsyncTask and post the location update
Updated answer
Old answer will not work for apps targeting M or higher, because static connectivity receiver will no longer receive broadcasts.
Now, the best option is a
FusedLocationApi
, which is a part of Google Play Services.Create an IntentService handling location updates
Don't forget to add it to Manifest application tag
Request location permission, connect GoogleApiClient, and you now may do the following
Old answer
AsyncTask is intended for short background operations, mostly when you are waiting in UI. You should better start a Service with WakeLock in this case. But be aware that you will likely drain a battery doing so every 15 minutes. Consider not scheduling Alarms when device is not connected to network.
1) Register a static BroadcastReceiver in Manifest to monitor if device is connected. Intent action for network events in android sdk
2) When device gets connected to the internet and locations are allowed, start a Service that will hold WakeLock, listen for one location update, unregisters location updates, sends location to server, schedules AlarmManager, releases WakeLock and cals stopSelf(); Consider a timeout if location updates are not comming. If timeout reached, unregister location updates, register next Alarm, release WakeLock and call stopSelf.
3) If you receive network disconnected in a Receiver, cancel all Alarms and stop Service if running.
Code sample for step 2 as requested