3

我正在尝试检查 GPS 和/或 WiFi 和移动网络位置是否已定位。我当前的代码仅适用于 GPS,并且我试图尝试包括网络提供商,但是我收到以下错误。

第一个错误

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


当前代码

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

4

2 回答 2

4

您必须分别检查每个提供商:

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();
    }
于 2013-07-11T03:12:36.873 回答
1

如果您看到isProvideEnabled(String)的文档,则只允许一个字符串作为参数。因此,您可以单独进行检查:

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

然后您可以按照@ianhanniballake 所说的或类似的方式检查它们:

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();   
    }
}

希望这可以帮助。

于 2013-07-11T03:13:28.363 回答