检查GPS和/或移动网络位置是否已启用

时间:2013-07-11 03:05:58

标签: java android gps locationmanager

我试图检查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();
        }

2 个答案:

答案 0 :(得分:3)

您必须单独检查每个提供商:

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

答案 1 :(得分:1)

如果您看到isProvideEnabled(String)的文档,则只允许一个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();   
    }
}

希望这有帮助。

相关问题