Hare-Krishna
Hare-Krishna

Reputation: 1425

Programmatically find device support GPS or not?

I am trying to find some code which will help me to find out if the device which I use has GPS or not? I don't want to know if GPS is enabled or disabled. I just want to know if the device has GPS hardware or not through my program.

Upvotes: 9

Views: 4816

Answers (3)

AxelH
AxelH

Reputation: 14572

Those methods are easier to use:

private boolean hasGpsSensor(){
        PackageManager packMan = getPackageManager();
        return packMan.hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS);
}
  • true: available (activated or not)
  • false: not available

So, in case of true, we can use

private boolean isGpsEnabled(){
        LocationManager manager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        return manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
  • true: enabled
  • false: disabled

With this two, you will know if GPS is available, activated or deactivated

Upvotes: 10

Fedor
Fedor

Reputation: 43412

There's also LocationManager.isProviderEnabled(String provider) method.

Upvotes: 3

Christopher Orr
Christopher Orr

Reputation: 111565

Yes, this can be done.

You can call LocationManager.getAllProviders() and check whether LocationManager.GPS_PROVIDER is included in the list.

Just for reference, I believe all released Android phones come with a GPS. It's not something that Android seem to be worrying about, e.g. mentioning GPS as one of the device attributes returned by PackageManager.getSystemAvailableFeatures().

Upvotes: 18

Related Questions