我无法使用以下代码打开或关闭 GPS,请告诉我是什么原因?

I can't Turn On Or Off GPS By Using Following Codes, Please Tell me What is the Reason?

private void turnGPSOn(){
    String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

    if(!provider.contains("gps")){ //if gps is disabled
        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);
    }
}

private void turnGPSOff(){
    String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

    if(provider.contains("gps")){ //if gps is enabled
        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);
    }
}

Android 指南在 4.0 版以上发生了变化。对于 4.0 以上的版本,您不能以编程方式关闭 GPS。但是,您可以通过编程方式更改 wifi 关闭。

应用程序无法以编程方式启用或禁用 GPS,除非是在已获得 root 权限的设备上。请允许用户这样做。