13

Possible Duplicate:
Programmatically find device support GPS or not?

How can I check, is GPS available on the device?

When I try to use the next code

PackageManager pm = context.getPackageManager();
boolean hasGps = pm.hasSystemFeature(PackageManager.FEATURE_LOCATION);

if (hasGps) {
    Log.d("TAG", "HAS GPS");
} else {
    Log.d("TAG", "HAS NOT GPS");
}

I always get HAS GPS, but my Archos 101IT has no GPS module. Is there a way to check hardware GPS availablity?

Community
  • 1
  • 1
skayred
  • 10,603
  • 10
  • 52
  • 94

2 Answers2

29

Use this code

boolean hasGps = pm.hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS);

if you have GPS hardware embedded for your device but it is not enabled then use the following cod to enable it dynamically,

LocationManager manager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
if(!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
    //Ask the user to enable GPS
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Location Manager");
    builder.setMessage("Would you like to enable GPS?");
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            //Launch settings, allowing user to make a change
            Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(i);
        }
    });
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            //No location service, no Activity
            finish();
        }
    });
    builder.create().show();
}

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

else you simply use this code LocationManager.isProviderEnabled(String provider) method.

if return false even in the case if you don`t have GPS in your device

Shabbir Dhangot
  • 8,954
  • 10
  • 58
  • 80
Karthi
  • 13,624
  • 10
  • 53
  • 76
  • 2
    I have a devide without GPS, but pm.hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS); always returns True – skayred Nov 03 '11 at 04:37
  • 3
    LocationManager.isProviderEnabled(String provider) return false for my tablet, that is true. But if I have GPS disabled on my handheld with GPS support, it will be false too. I need to check hardware availability, not enabled-disabled state – skayred Nov 03 '11 at 06:03
  • Have you getAllProviders from your device and check whether GPS provider is present. – Karthi Nov 03 '11 at 06:06
  • 1
    When GPS on my other device is enabled, GPS provider is presented, but when I disable it, there is only network. So, I cannot use that method – skayred Nov 03 '11 at 06:11
  • I searched so far , i didn`t find any way because class and packages are common for android OS Ex:android 2.2 not for any specific device. – Karthi Nov 03 '11 at 06:21
  • I didn`t find any class or package exist for that to detect the hardware – Karthi Nov 03 '11 at 06:22
  • instead of `startActivity` use `startActivityForResult` to handle if user activates Gps or not, from settings – Jemshit Nov 23 '15 at 14:58
  • This code seems to check for _hardware_ capability, not just whether it's enabled or disabled: `boolean hasGps = pm.hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS);` Exactly what I needed, thanks! – Joshua Pinter Sep 26 '18 at 01:06
2

The android systems GPS setting screen can be called just like any other Activities:

startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);

To check GPS availability use the code code below:

(the Activity must implement LocationListener)

LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L,1.0f, this);
boolean isGPS = locationManager.isProviderEnabled (LocationManager.GPS_PROVIDER);
  • How coming when toggling my gps off and on isGPS from your code snippet is always true? Are you sure this detects if GPS is active, or does it check if GPS is available or permitted or other? – portfoliobuilder Sep 15 '16 at 17:26