如何在Android 4.4中以编程方式启用/禁用GPS?

如何在Android 4.4中以编程方式启用/禁用GPS?,android,gps,Android,Gps,我使用此代码启用GPS,但它会给我这样的错误 Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE"); intent.putExtra("enabled", true); getBaseContext().sendBroadcast(intent);` 所以任何人都给我一个解决办法。你从来没有这样做过。这是API中的错误。现在,您可以使用以下意图为用户提供选择设置: java.lang.SecurityExceptio

我使用此代码启用GPS,但它会给我这样的错误

Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
getBaseContext().sendBroadcast(intent);`

所以任何人都给我一个解决办法。

你从来没有这样做过。这是API中的错误。现在,您可以使用以下意图为用户提供选择设置:

java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.location.GPS_ENABLED_CHANGE from pid=1208, uid=10051

您无法在android中以编程方式启用/禁用GPS。您可以要求用户启用/禁用它。请在墙纸服务中使用此代码,以便在该服务中无法打开activity.Settings.ACTION\u LOCATION\u SOURCE\u Settings及其包名?
startActivity(context, new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
public void turnGPSOn()
     {
     Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
     intent.putExtra("enabled", true);
     this.ctx.sendBroadcast(intent);

     String provider = Settings.Secure.getString(ctx.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"));
     this.ctx.sendBroadcast(poke);
     }