GPS isProviderEnabled总是返回false

Lau*_*Lau 10 provider gps android return

我有这个代码

 lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
 boolean isGPS = lm.isProviderEnabled (LocationManager.GPS_PROVIDER);
Run Code Online (Sandbox Code Playgroud)

即使启用GPS,它也始终返回false.GPS正常工作,但我使用此布尔值显示弹出"没有启用GPS".在这种情况下,弹出窗口每次都会出现

  1. 我检查了类似的问题,但它对我没有帮助.
  2. 是的,我有权在我的清单中
  3. 我在onResume方法中使用此代码

感谢帮助.

Muh*_*aat 25

如果您正在检查位置是否打开或不使用GPS,那么您必须注意以下情况,因为在我的情况下,在设备位置设置中,定位方法设置为BatterySaving模式,其中设备仅使用WiFi移动网络来估算位置:

在此输入图像描述

因此,GPS甚至不用于更新位置,因此位置图标不会出现在状态栏中,除了位置提供者将被称为网络而不是GPS.

因此,要解决该问题,您必须检查提供程序是否包含gps或包含network:

private boolean checkIfLocationOpened() {
    String provider = Settings.Secure.getString(getActivity().getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
    if (provider.contains("gps") || provider.contains("network"))
        return true;
    }
    // otherwise return false
    return false;
}
Run Code Online (Sandbox Code Playgroud)

如果你想用以下方法做到LocationManager:

private boolean checkIfLocationOpened() {
    final LocationManager manager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
    if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER) || manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
        return true;
    }
    // otherwise return false
    return false;
}
Run Code Online (Sandbox Code Playgroud)


Din*_*ris 9

试试这种方式..

private void turnGPSOn(){   
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);   
if(!provider.contains("gps")){      
    final Intent poke = new Intent();  
    poke.setClassName("com.android.settings","com.android.settings.widget.SettingsAppWidgetProvider");           
    poke.addCategory(Intent.CATEGORY_ALTERNATIVE);   
    poke.setData(Uri.parse("3"));      
    sendBroadcast(poke);  
    } 
}  
Run Code Online (Sandbox Code Playgroud)