Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android check GPS availability on device [duplicate]

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?

like image 659
skayred Avatar asked Nov 03 '11 04:11

skayred


1 Answers

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

like image 172
Karthi Avatar answered Oct 01 '22 13:10

Karthi