检查是否启用了GPS和/或移动网络位置
我试图检查移动网络位置是否位于GPS和/或WiFi &。我目前的代码只适用于GPS,我试图尝试包括网络提供商,但我收到以下错误。检查是否启用了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();
}
你必须检查每个单独提供:
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();
}
查看更新错误 –
它说你在org.greenbot.technologies.gpsme.MainActivity有一个空指针。
如果您看到isProvideEnabled(String)的文档,则只允许一个字符串作为参数。所以,你可以做seperately的检查:
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();
}
}
希望这有助于。
感谢您的代码,这是有用的 –
这实际上会导致布尔值的nullpointerexception –
如果提供程序为空,则isProviderEnabled()返回'IllegalArgumentException'。它在哪一行显示空指针异常? –
你没有使用新的[Fused Location provider](https://developer.android.com/google/play-services/location.html)的原因?它提供的位置更快,更节能,并将所有提供商组合在一起。 – ianhanniballake