Android: LocationListener and getLastKnownLocation - android

I have a Service which is running every minute and is supposed to constantly track location. The Service implements LocationListener. I feel like I'm not understanding this correctly though because I'm not actually using the LocationListener methods:
onLocationChanged
onProviderEnabled
onProviderDisabled
etc
I have those methods in my Class but I don't actually do anything with them. All I'm doing is every time my service runs I call LocationManager.getLastKnownLocation for both GPS and Network Provider depending on which ones are available.
This is what my code looks like:
Service runs every minute:
Intent i = new Intent(context, GPSTracker.class);
PendingIntent pi = PendingIntent.getService(context, 0, i, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if(isOn) {
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), POLL_INTERVAL, pi);
} else {
alarmManager.cancel(pi);
pi.cancel();
}
I handle the Intent:
protected void onHandleIntent(Intent intent) {
Log.i(TAG, "In onHandleIntent!");
Location currentLocation = getLocation(this);
if(currentLocation == null) {
saveLocation(-8.0, -8.0, "nothing", "nothing", "nothing");
} else {
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
try {
addresses = geocoder.getFromLocation(latitude, longitude, 1);
String city = addresses.get(0).getLocality();
String country = addresses.get(0).getCountryCode();
String state = addresses.get(0).getAdminArea();
saveLocation(currentLocation.getLatitude(), currentLocation.getLongitude(), city, state, country);
} catch (IOException e) {
e.printStackTrace();
}
}
}
And getLocation() does the real work:
public Location getLocation(Context context) {
try {
locationManager = (LocationManager) context
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
Something doesn't seem right though. For one, like I said I don't use any LocationListener methods, and also it doesn't seem to be very accurate. When I test on my phone sitting at my computer desk it gets it spot on every time. However, today I had this running on my phone once at work and another time when I was walking down the street and the locations it got for these places was like a mile away from both of them. Weirdly, it got the exact same wrong location for both of these places. These were both with Network providers as I didn't have my GPS on.
What am I doing wrong, and if I'm not doing anything wrong, how can I get this to be more accurate?

getLastKnownLocation () will provide the last known location(not the current location).The location could be out-of-date, if the device was turned off and moved to another location.
You need to use LocationClient to get the current location using Google Location Service. It simple to integrate. You need to create a instance to LocationClient and connect. There are callback method when the connection is established.Once connected you can retrieve the current location from mLocationClient.getLastLocation() (Returns the best most recent location currently available).
check this link https://developer.android.com/training/location/retrieve-current.html

As your concern is to get more accurate result then you should use GPS. You can simply add a GPS enable button to ask the user for turning on the GPS.
gpsButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
isGpsON = true;
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});
You didn't use any LocationListener but I think you should use that to take the location update on
#Override
public void onProviderEnabled(String provider) {
getLocation(this);
}

Related

why mobile phone gps provide sometime same coordinates on different location

Background: I am now working on an Android app to track vehicles where I required to get device location on every 10 second as on management decision. We are already developed this app which provides us updated location on time as our expected time interval.
Problem: When Vehicles are driving on the roads, the app gives us correct coordinates as our expected time interval, but sometime 5-20 minutes it provide same coordinates yet the vehicle is in a different location.
Code Review: 1) We use a method named getLocation() inside LocationClass that provide us latitude longitude of a device current location.
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 5;
private static final long MIN_TIME_BW_UPDATES = 1000*20;
public Location getLocation(Context context) {
Location location=null;
try {
locationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(context,"Please! Provide Location Access Permission.....", Toast.LENGTH_LONG).show();
return TODO;
}
// if GPS Enabled get lat/long using GPS Services
if(isGPSEnabled) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
location.setProvider("GPS");
}
}
}
if (isNetworkEnabled && location==null) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
location.setProvider("AGPS");
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
2) We repeat this method using a timer on a service named GPSTracker at fixed time interval.
public class GPSTracker extends Service implements LocationListener {
private Timer mTimer = null;
private Handler mHandler = new Handler();
Location location;
#Override
public void onCreate() {
super.onCreate();
if (mTimer != null) {
mTimer.cancel();
} else {
// recreate new
mTimer = new Timer();
}
// schedule task
mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 10 * 1000, 10 * 1000);
}
class TimeDisplayTimerTask extends TimerTask {
#Override
public void run() {
// run on another thread
mHandler.post(new Runnable() {
#Override
public void run() {
location=new Location("");
LocationCls cls = new LocationCls();
location = cls.getLocation(getBaseContext());
}
});
}
}
}
I am trying to solve this issue a 2 long day but not found any coding problem or solution yet. Any Solution or Suggestion is highly appreciate.
Thanks in advance....

Get user current lat lng after GPS enabled - Android

I have followed this tutorial http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/ for showing popup if GPS in not enabled and getting users current location.
And here is the broadcast receiver which listens to the GPS enabled disabled condition
private BroadcastReceiver mGpsSwitchStateReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
gps = new GPSTracker(getActivity());
if(gps.canGetLocation()){
Log.d("Your Location", "latitude:" + gps.getLatitude() + ", longitude: " + gps.getLongitude());
Toast.makeText(getActivity(), "GPS Switch", Toast.LENGTH_SHORT).show();
if(gps.getLatitude() != 0 && gps.getLongitude() != 0 ){
final CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(gps.getLatitude(),gps.getLongitude())) // Sets the center of the map to selected place
.zoom(15) // Sets the zoom
.build(); //
gMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
Toast.makeText(getActivity(), "GPS Switch Lat: "+gps.getLatitude()+" Long: "+gps.getLongitude(), Toast.LENGTH_SHORT).show();
}
} else {
if(!gps.isSettingDialogShown())
gps.showSettingsAlert();
}
}
}
};
When I enable the GPS via settings dialog or from drop down menu. GPSTracker class gets the location service and checks for the user's current location in this code fragment
public Location getLocation() {
try {
locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network Enabled");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS", "GPS Enabled");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (SecurityException e) {
e.printStackTrace();
}
return location;
}
Problem:
Firstly I don't know why broadcast receiver, receives the action two times and secondly when in normal mode lat lng value in receiver remains zero but in debugging sometimes it returns the value of users current location.
Can you please help what I am doing wrong?
If you don't need to work with Gps and Network Locations separately. I recommend to use FusedLocationProvider. BroadcastReceiver is probably working twice because it works for both gps and network location services

Android GPS networkProvider not working

In my application, I tried to search the coarse location of the android device using the networkProvider. I only use the networkProvider in my location manager, but it won't work if I don't turn on the GPS sensor on.
Is the networkprovider supposed to give a coarse location no matter the GPS sensor is on or off?
Here's my code.
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
if(isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
Log.e("Network","suc");
else
Log.e("Network", "fail");
if (!isNetworkEnabled) {
} else {
this.isGetLocation = true;
if (isNetworkEnabled) {
Log.e("GpsInfo", "isNetworkEnabled true");
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null) {
Log.e("GpsInfoClass", "Location manager not NULL");
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
lat = location.getLatitude();
lon = location.getLongitude();
Log.e("GpsInfoClass", lon + ", " + lat );
}else{
Log.e("GpsInfoClass", "Location NULL");
}
}else{
Log.e("GpsInfo", "Location Manager is null");
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
In my log, it prints Network(tag) fail(log).
What this mean is that,
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
always returns false, if the GPS sensor is off. Why is this happening? What am I missing about getting coarse locations?
Is the networkprovider supposed to give a coarse location no matter the GPS sensor is on or off?
No, the GPS radio has nothing to do with getting Network Location.
However, you need to have your Location Settings set to either Power Saving or High Accuracy.
Using this code to show a Toast with the current enabled providers:
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Toast.makeText(context, "GPS Enabled: " + isGpsEnabled + " Network Location Enabled: " + isNetworkEnabled, Toast.LENGTH_LONG).show();
Here is what it shows for GPS only and Power Saving:
So, you can see that this call:
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Returns false for the GPS Only setting, since that disables Network Location.
And, it returns true for the Power Saving setting.
It would also return true for High Accuracy as well, since that enables both GPS and Network Location.
GPS sensor only needed for exact location. If you create a Criteria and give it to locationManager, system automatically selects best provider and tries get coordinates.
Example;
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria locationMode = new Criteria();
locationMode.setBearingRequired(false);
locationMode.setSpeedRequired(true);
locationMode.setAccuracy(Criteria.ACCURACY_MEDIUM);
locationManager.requestSingleUpdate(locationMode, new LocationListener() {#Override
public void onLocationChanged(Location location) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
}, null);
}

Location only refreshes when I turn GPS off

So I have a problem when trying to resolve my location. When given the command to find my location, it instead gives me the last location of where I turned my GPS off. Being able to find my coarse location using WiFi also seems to not be working.
Here is my current class
public class LocationTracker extends Service implements LocationListener {
//flag for GPS Status
boolean isGPSEnabled = false;
//flag for network status
boolean isNetworkEnabled = false;
boolean canGetLocation = false;
Location location;
double latitude;
double longitude;
//The minimum distance to change updates in metters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; //10 metters
//The minimum time beetwen updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
//Declaring a Location Manager
protected LocationManager locationManager;
public void fetchLocation(Context context) {
contextnormal = context;
getLocation(context);
if (canGetLocation())
{
String stringLatitude = String.valueOf(latitude);
String stringLongitude = String.valueOf(longitude);
Log.i("Location: ", stringLatitude + " " + stringLongitude);
}
else
{
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
Log.i("Error: ", "Cannot get location");
}
}
public Location getLocation(Context context)
{
try
{
locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
//getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
//getting network status
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled)
{
// no network provider is enabled
}
else
{
this.canGetLocation = true;
//First get location from Network Provider
if (isNetworkEnabled)
{
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null)
{
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
updateGPSCoordinates();
}
}
//if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled)
{
if (location == null)
{
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null)
{
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
updateGPSCoordinates();
}
}
}
}
}
catch (Exception e)
{
//e.printStackTrace();
Log.e("Error : Location", "Impossible to connect to LocationManager", e);
}
return location;
}
public void updateGPSCoordinates()
{
if (location != null)
{
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
/**
* Stop using GPS listener
* Calling this function will stop using GPS in your app
*/
public void stopUsingGPS()
{
if (locationManager != null)
{
locationManager.removeUpdates(LocationTracker.this);
}
}
/**
* Function to get latitude
*/
public double getLatitude()
{
if (location != null)
{
latitude = location.getLatitude();
}
return latitude;
}
/**
* Function to get longitude
*/
public double getLongitude()
{
if (location != null)
{
longitude = location.getLongitude();
}
return longitude;
}
/**
* Function to check GPS/wifi enabled
*/
public boolean canGetLocation()
{
return this.canGetLocation;
}
#Override
public void onLocationChanged(Location location)
{
//Want to Execute Asynctask method to HTTP post the lat and long. Is there a way `to pass in a context to do this?`
}
#Override
public void onProviderDisabled(String provider)
{
}
#Override
public void onProviderEnabled(String provider)
{
if (contextnormal != null) {
fetchLocation(contextnormal);
}
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
#Override
public IBinder onBind(Intent intent)
{
return null;
}
I would guess the problem lies in these lines
if (isGPSEnabled)
{
if (location == null)
{
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null)
{
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
updateGPSCoordinates();
}
}
And for some reason it is giving the last known location as the last time the GPS was turned on. Any ideas as to why it is doing that instead of giving the last known location as the present location if the GPS is on?
EDIT:
Implementing the following method and testing:
#Override
public void onLocationChanged(Location location)
{
double newLat = location.getLatitude();
double newLong = location.getLongitude();
String stringNewLatitude = String.valueOf(newLat);
String stringNewLongitude = String.valueOf(newLong);
new MyAsyncTask().execute(stringNewLatitude, stringNewLongitude);
}
You can't just query the location manager for its last known location repeatedly, that will only get you the last known location. It sounds like what you are trying to do is get location updates as you move around.
In this case, you will need to register for location updates, as you tried with your line:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
You are on the right track here, but what is this (the last argument on the function call [yes I know it is a reference to the current class...])? The last argument is supposed to be an intent or a callback where the updates will be broadcast to. It doesn't appear that you have implemented that from the code you provided.
Change your this argument to a class which implements LocationListener. The location updates will come to the onLocationChanged method where you can then process them as you see fit. A simple way to do this is to add a nested inner class which implements the listener, then you can just create an instance of that class and pass it to the location manager, in place of your current this argument.
EDIT
OK, so you are implementing the thing correctly, but blisfully ignoring the location updates, don't do that. The service you have is a Context -- so you can pass that around as you see fit, although it may be better to use:
this.getApplicationContext();
If you are passing it to some long running thing. What I would recommend is to queue your location updates when the arrive in onLocationChanged(). After queuing the location, signal a background worker thread (or AsyncTask) to actually post the location to your web service.
Also, don't override onBind and return null. Just don't override it.

Why doesn't onLocationChanged method get called in service?

I am getting my current location in the form of latitudes and longitudes. My problem is that once I get the coordinates it doesn't get updated when I move to another location.I think onLocationChanged method is not getting called. I read here http://www.lengrand.fr/2013/10/onlocationchanged-is-never-called-on-android/ that it doesn't get called by itself. I have gone through most of the tutorials and can't find a solution.So how can I call onLocationChanged method in my service. Please suggest me step by step.
My codes are as follow:
public class GPSTracker extends Service implements LocationListener {
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
}
#Override
public void onLocationChanged(Location location) {
getLocation();
}
Make sure you requestLocationUpdates(...) from the LocationManager (which in your case it seems you do)
If your inside a building and your device doesn't get GPS fix onLocationChanged(...) is not called, I had the same issue (when debugging in my home) but going outside I saw that the function is called once GPS fix is retrieved.
try
#Override
public void onLocationChanged(Location location)
{
Toast.makeText(getApplicationContext(), "My Position !!!"+ location.getLatitude + location.getLongitude,
Toast.LENGTH_LONG).show();
}
It is weird that you are even getting a single location update. From you code it looks like you call getLocation() when you recieve a location change, but you then only setup your location requests inside of the getLocation() function (i.e. you should never enter the getLocation() function). This is how I implemented a location listener for gps data:
Note: I used this in a service, but I did not have my service implement LocationListener.
private void setupLocationUpdates() {
// Acquire a reference to the system Location Manager
mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
mLocationListener = new LocationListener() {
// Called when the location has changed.
public void onLocationChanged(Location location) {
mLocation = location;
}
}
// Called when the provider is disabled by the user.
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d(TAG, "Status Changed: " + String.valueOf(status) + " Provider:" + provider);
}
// Called when the provider is enabled by the user.
public void onProviderEnabled(String provider) {
if(provider.equalsIgnoreCase("gps")) setNotification(Constants.GPS_ENABLED);
Log.d(TAG, "Provider: " + provider + " ENABLED");
}
// Called when the provider status changes.
public void onProviderDisabled(String provider) {
if(provider.equalsIgnoreCase("gps")) setNotification(Constants.GPS_DISABLED);
Log.d(TAG, "Provider: " + provider + " DISABLED");
}
};
mlocationProvider = LocationManager.GPS_PROVIDER;
// Register for location updates
mLocationManager.requestLocationUpdates(mlocationProvider, 0, 0, mLocationListener);
// Add a GPS Listener to notify when searching for signal and when fix is acquired
mGpsListener = new GpsListener();
mLocationManager.addGpsStatusListener(mGpsListener);
}

Categories

Resources