Reputation: 570
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.
Upvotes: 3
Views: 5815
Reputation: 570
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
}
Upvotes: 4