Jaison Brooks
Jaison Brooks

Reputation: 5826

Check if GPS and/or Mobile Network location is enabled

Im trying to check whether GPS and/or WiFi & Mobile network location are located. My current codes simply works for just GPS, and I've attempted to try to include the network provider, however i've getting the following error.

First Error

The method isProviderEnabled(String) in the type LocationManager is not applicable for the arguments (String, String)


Current Code

    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER, LocationManager.NETWORK_PROVIDER)){
            Toast.makeText(this, "GPS is Enabled in your device", Toast.LENGTH_SHORT).show();
        }else{
            displayAlert();
        }

Upvotes: 3

Views: 1517

Answers (2)

Shobhit Puri
Shobhit Puri

Reputation: 26007

If you see documentation of isProvideEnabled(String), only one String is allowed as parameter. So you can do the checking seperately:

boolean gpsPresent = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean networkProviderPresent = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

Then you can check them as @ianhanniballake said or something like:

if ( (!gpsPresent) && (!networkProviderPresent) ){
     displayAlert(); // Nothing is available to give the location
}else {
    if (gpsPresent){
        Toast.makeText(this, "GPS is Enabled in your device", Toast.LENGTH_SHORT).show();    
    }
    if (networkProviderPresent ){
        Toast.makeText(this, "Network Provider is Present on your device", Toast.LENGTH_SHORT).show();   
    }
}

Hope this helps.

Upvotes: 1

ianhanniballake
ianhanniballake

Reputation: 199795

You have to check each provider separately:

if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
    locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
        Toast.makeText(this, "GPS/Network is Enabled in your device", 
            Toast.LENGTH_SHORT).show();
    }else{
        displayAlert();
    }

Upvotes: 4

Related Questions