Showing posts with label Location. Show all posts
Showing posts with label Location. Show all posts

How to Determine if Android Device has GPS Support

public static final String FEATURE_LOCATION_GPS was added in API level 8
Feature for getSystemAvailableFeatures() and hasSystemFeature(String).
The device has a Global Positioning System receiver and can report precise location.
Constant Value: "android.hardware.location.gps"

1. Get a PackageManager Instance and save to myPackageManager.

2. Create a Boolean Variable named bolGPSSupported

3. Set the Minimum and Target SDK version  to or greater. 

4. In the onCreate method, add the below code. This will return the GPS ability to a boolean named bolGPSSupported. It will then display a toast displaying the GPS ability on the device to the user.

bolGPSSupported = myPackageManager.hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS);
if (bolGPSSupported) {
Toast.makeText(this, "Device has GPS feature", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "No GPS feature!", Toast.LENGTH_LONG).show();
}

5. Compile and run!

Resources:
http://developer.android.com/reference/android/content/pm/PackageManager.html#FEATURE_LOCATION_GPS

How to Determine if GPS of Android Device is enabled/disabled

public boolean isProviderEnabled (String provider) was added in API level 1
Returns the current enabled/disabled status of the given provider.
If the user has enabled this provider in the Settings menu, true is returned otherwise false is returned
Callers should instead use LOCATION_MODE unless they depend on provider-specific APIs such as requestLocationUpdates(String, long, float, LocationListener).
Parameters: provider the name of the provider
Returns: true if the provider exists and is enabled
Throws:
IllegalArgumentException -  if provider is null
SecurityException - if no suitable permission is present

1. In the MainActivity.java file, Get a Reference to LocationManager

2. Get the Name of the GPS Location Provider named myProvider.

3. In the onCreate method, add the following code. This will test if the GPS is enabled or disabled. You can optionally Add a Toast.

try {
    if (myLocationManager.isProviderEnabled(myProvider)) {
        //Toast.makeText(this, "GPS is Enabled",  Toast.LENGTH_LONG).show(); 
    } else {
        //Toast.makeText(this, "GPS is Disabled",  Toast.LENGTH_LONG).show(); 
    }
} catch( IllegalArgumentException e) {
    e.printStackTrace();
    //Toast.makeText(this, "isProviderEnabled - IllegalArgumentException",        Toast.LENGTH_LONG).show(); 
} catch( SecurityException e) {
    e.printStackTrace();
    //Toast.makeText(this, "isProviderEnabled - SecurityException",  Toast.LENGTH_LONG).show(); 
}
4. Compile and run!

Resources: 

Other Resources:

Learning Android Application Programming: A Hands-On Guide to Building Android Applications

How to Get the Longitude

public double getLongitude () was added in API level 1
Get the longitude, in degrees.
All locations generated by the LocationManager will have a valid longitude.

1. In the MainActivity.java file, Get the Last Known Location

2. Declare a Double variable named dblLongitude

3. In the onCreate method, add the below line. This test and make sure the myLocation object is not null, and if not null, will get the longitude, in degrees and save it to a double variable named dblLongitude. Note: This will be the Last Known Longitude. It may not be the current longitude.

if (myLocation !=null) {
    dblLongtidue = myLocation.getLongtitude();
}




How to Get the Latitude

public double getLatitude () was added in API level 1
Get the latitude, in degrees.
All locations generated by the LocationManager will have a valid latitude.

1. In the MainActivity.java file, Get the Last Known Location

2. Declare a Double variable named dblLatitude

3. In the onCreate method, add the below line. This test and make sure the myLocation object is not null, and if not null, will get the latitude, in degrees and save it to a double variable named dblLatitude. Note: This will be the Last Known Latitude. It may not be the current latitude.

if (myLocation !=null) {
    dblLatitude = myLocation.getLatitude();
}




How to Declare Location object

Location was added in API Level 1
A data class representing a geographic location.
A location can consist of a latitude, longitude, timestamp, and other information such as bearing, altitude and velocity.
All locations generated by the LocationManager are guaranteed to have a valid latitude, longitude, and timestamp (both UTC time and elapsed real-time since boot), all other parameters are optional.

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

import android.location.Location;

2. Add the below line to the onCreate method. This will declare a Location object named myLocation.
Location myLocation; 

3. Compile and run!

Next Recommended Article: How to Get the Last Known Location

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