I need to send latitude and longitude values to the server for every 5 minutes.Our team members can be close this application during the travels that time also latitude and longitude value send to the server.I didn't have idea to how to implement this method.
Asked
Active
Viewed 5,725 times
3
-
3create background services fetch location and send it to server simple! – Dhaval Parmar Jan 28 '16 at 06:38
-
@DhawalSodhaParmar This is first time I am working with background process.I haven't enough knowledge in this area.I know get the latitude and longitude but how send the data to server when app was in close. – Gopal Jan 28 '16 at 06:40
-
1lets made easy for you. first learn about background services https://github.com/codepath/android_guides/wiki/Starting-Background-Services , then how get location from device http://stackoverflow.com/questions/17519198/how-to-get-the-current-location-latitude-and-longitude-in-android and http://developer.android.com/intl/ru/training/location/display-address.html in last send on server(i think this you already know) – Dhaval Parmar Jan 28 '16 at 06:44
-
@DhawalSodhaParmar thank you... – Gopal Jan 28 '16 at 06:45
-
some stack answer please check but before use it learn abut them from previous comment http://stackoverflow.com/a/28535885/1168654 and http://stackoverflow.com/q/14478179/1168654 – Dhaval Parmar Jan 28 '16 at 06:48
1 Answers
4
Finally I found an answer for my question. We can run the background process many methods like Intent Service and Alarm Manager. I used the Alarm Manager for Background Process.It will call service every 5 minutes.In my MainActivity
,I put the below mentioned Code.
private void scheduleNotify()
{
Intent notificationIntent = new Intent(this, SendLocation.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,0,1000*60*5,pendingIntent);
}
In AlarmManager
,I set the setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,0,1000*60*5,pendingIntent);
It will repeating the every 5 minutes duration(1000*60*5 ms = 5minutes)
SendLocation.class
public class SendLocation extends BroadcastReceiver implements LocationListener {
@Override
public void onReceive(Context mContext, Intent intent) {
try {
locationManager = (LocationManager) mContext.getSystemService(mContext.LOCATION_SERVICE);
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES,this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}catch (Exception e){
}
Log.i("sendlocation","Every 5 minutes it will appear in Log Console"+latitude+" "+longitude);
// code for send location to srver
}

Gopal
- 580
- 8
- 26
-
For Better performance switch to fused Location than location manager – androidXP Sep 13 '16 at 07:36