Showing posts with label LocationManager. Show all posts
Showing posts with label LocationManager. Show all posts

How to Register a Passive Provider for Location Updates

public void requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener) was added in API level 1
Register for location updates using the named provider, and a pending intent.
Parameters:
provider - the name of the provider with which to register
minTime - minimum time interval between location updates, in milliseconds
minDistance - minimum distance between location updates, in meters
listener - a LocationListener whose onLocationChanged(Location) method will be called for each location update
Throws
IllegalArgumentException -  if provider is null or doesn't exist on this device
IllegalArgumentException - if listener is null
RuntimeException - if the calling thread has no Looper
SecurityException - if no suitable permission is present
** A passive provider receives location updates if, and only if, another application requests them, letting your application passively receive location updates without activating any Location Provider.

1. Get a Reference to LocationManager named myLocationManager

2. Get the Passive Provider and save to a string variable named myPassiveProvider.

3. Add a LocationListener named myLocationListener.

4. Add the below line to the onCreate method. This will request the Passive Provider named myPassiveProvider to request location updates every 0 milliseconds, and 0 meters and use the LocationListener named myLocationListener for the results.

myLocationManager.requestLocationUpdates(myPassiveProvider, 0, 0, myLocationListener);

5. Compile and run!

Resources:
Professional Android 4 Application Development by Reto Meier, pg 525
http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates(java.lang.String, long, float, android.location.LocationListener)

How to Get the Passive Provider

public static final String PASSIVE_PROVIDER was added in API level 8
A special location provider for receiving locations without actually initiating a location fix.
This provider can be used to passively receive location updates when other applications or services request them without actually requesting the locations yourself. This provider will return locations generated by other providers. You can query the getProvider() method to determine the origin of the location update. Requires the permission ACCESS_FINE_LOCATION, although if the GPS is not enabled this provider might only return coarse fixes.

1. Ensure to change the minSDKVersion to 8, or greater.

2. Set Permission to Allow Location Information

3. Declare a String Variable named myPassiveProvider.

4. Add the LocationManager class to the imports section.

5. Add the below line to the onCreate method. This will save the Passive Provider to the string variable myPassiveProvider.

myPassiveProvider = LocationManager.PASSIVE_PROVIDER;

6. Compile and run!

Resources:
Professional Android 4 Application Development by Reto Meier, pg 525
http://developer.android.com/reference/android/location/LocationManager.html#PASSIVE_PROVIDER

How to Get the Last Known Location

public Location getLastKnownLocation (String provider) was added in API level 1
Returns a Location indicating the data from the last known location fix obtained from the given provider.
This can be done without starting the provider. Note that this location could be out-of-date, for example if the device was turned off and moved to another location.
If the provider is currently disabled, null is returned.
The GPS values returned by getLastKnownLocation do not change unless at least one application requests location updates.
** getLastKnownLocation does not ask the Location Provider to update the current position. If the device has not recently updated the current position, this value may not exist or be out of date. **
Parameters: provider - the name of the provider
Returns: the last known location for the provider, or null
Throws:
SecurityException - if no suitable permission is present
IllegalArgumentException - if provider is null or doesn't exist

Note: In the settings (Location) of the Android Device, the Use GPS satellites needs to be enabled for this to work.

1. In the MainActivity.java file, Declare Location object named myLocation.

2. In the onCreate method,  Determine if GPS of Android Device is enabled/disabled

3. Add the below code between the enabled brackets from above. This will set the myLocation Location object to the Last Known Location, or to null if not available. If the GPS is not enabled, this code below will not work correctly.

try { 
    myLocation = myLocationManager.getLastKnownLocation(myProvider);
} catch (SecurityException e) { 
e.printStackTrace();
    //Toast.makeText(this, "getLastKnownLocation.SecurityException", Toast.LENGTH_LONG).show();
} catch (IllegalArgumentException e) {

e.printStackTrace();
    //Toast.makeText(this, "getLastKnownLocation.IllegalArgumentException", Toast.LENGTH_LONG).show();
}

4. Compile and run!

Resources:

How to Get the Name of the GPS Location Provider

public static final String GPS_PROVIDER was added in API level 1
Name of the GPS location provider.
This provider determines location using satellites. Depending on conditions, this provider may take a while to return a location fix. Requires the permission ACCESS_FINE_LOCATION.
The extras Bundle for the GPS location provider can contain the following key/value pairs:
satellites - the number of satellites used to derive the fix

1. Set Permission to Allow Location Information

2. In the MainActivity.java file, Get a Reference to LocationManager named myLocationManager.

3. Add the below line to the onCreate method. This will save the name of the GPS location provider to a string named myProvider.

String myProvider = LocationManager.GPS_PROVIDER;

4. Compile and run!

Next Recommended Article: How to Declare Location object

Resources:
http://developer.android.com/reference/android/location/LocationManager.html#GPS_PROVIDER


How to Get a Reference to LocationManager

public abstract Object getSystemService (String name) was added in API level 1
Return the handle to a system-level service by name. The class of the returned object varies by the requested name.

1. In the MainActivity.java file, Get the Context.LOCATION_SERVICE and save to a string named svcName.

2. In the onCreate method, Declare a LocationManager named myLocationManager.

3. In the onCreate method, add the following line. This will get a reference to the LocationManager saved to myLocationManager.

myLocationManager = (LocationManager)getSystemService(svcName);

4. Compile and run!

Next Recommended Article: How to Get the Name of the GPS Location Provider

Resources:
http://developer.android.com/reference/android/content/Context.html#getSystemService(java.lang.String)

How to Declare a LocationManager

LocationManager was added in API Level 1
This class provides access to the system location services. These services allow applications to obtain periodic updates of the device's geographical location, or to fire an application-specified Intent when the device enters the proximity of a given geographical location.
You do not instantiate this class directly; instead, retrieve it throughContext.getSystemService(Context.LOCATION_SERVICE).

1. In the MainActivity.java file, add the below line to the import section.

import android.location.LocationManager;

2. Add the below line to the onCreate method. This will declare a LocationManager named myLocationManager.

LocationManager myLocationManager;

3. Compile and run!

Next Recommended Article: How to Get a Reference to LocationManager

Resources:
http://developer.android.com/reference/android/location/LocationManager.html