In my app, i need to create an android service to periodically get mobile user's current location(latitude and longitude). The service should get user's location in every 5 minutes.. How can i do this in android service that would run in background. And is this the right approach?
Asked
Active
Viewed 3,432 times
2
-
@Reno: Maybe, it all depends on what he uses it for and the promises he makes to his users. Information is neither good or bad; just the use of that information can be. – Robert Massaioli Mar 03 '11 at 11:03
-
what exactly is the problem. thats a long question... – Farhan Mar 03 '11 at 11:07
-
In my app, i need to create an android service to periodically get mobile user's current location(latitude and longitude). The service should get user's location in every 5 minutes. – avdroidDev Mar 03 '11 at 11:25
3 Answers
0
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mlocListener);
}
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
Log.i("**************************************", "Location changed");
loc.getLatitude();
loc.getLongitude();
String Text = "My current location is: " +"Latitude = " + loc.getLatitude() + "Longitude = " + loc.getLongitude();
Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show();
}
@Override
public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}

Martijn Pieters
- 1,048,767
- 296
- 4,058
- 3,343

Ankitkumar Makwana
- 3,475
- 3
- 19
- 45
0
You don't need a service to accomplish this. In order to get location every 5mins, in your app, on the activity you can implement the location listener. While setting up the location manager, you can specify the minTime = 5mins (300000 ms)
. The onlocationChanged
method will be called every 5 mins or when the distance change > minDistance
, which ever occurs first.
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
MyLocationListener listener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 300000, minDistance, listener);

Adil Soomro
- 37,609
- 9
- 103
- 153

lini sax
- 911
- 1
- 11
- 15
0
You can write a private/internal class(within your service) that implements locationListener and in the onStart meethod of your service you can requestLocationUpdates which will be listened by your listener
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
MyLocationListener listener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minDistance, listener);

mixkat
- 3,883
- 10
- 40
- 58