可能重复,
如何以编程方式在android中启用/禁用gps和移动数据? https://stackoverflow.com/questions/16752859/how-to-enable-disable-gps-and-mobile-data-in-android-programmatically
我一直看到人们说他们能够以编程方式打开 Android 中的 GPS。但我使用相同的代码却无法做到这一点。
它只是显示“搜索 GPS ..”,但实际上并没有这样做。
这是我正在使用的代码,
//Enable GPS
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
sendBroadcast(intent);
//Disable GPS
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", false);
sendBroadcast(intent);
我也尝试过这个,
String provider = Settings.Secure.getString(context.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"));
context.sendBroadcast(poke);
}
但当我这么做的时候,
LocationManager manager = (LocationManager) getSystemService( getApplicationContext().LOCATION_SERVICE );
boolean isGPSEnabled = manager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
它返回我GPS 已启用一如既往的虚假。如果我手动打开 GPS,它会返回 true。
谁能告诉我是否可以像这样打开 GPS?如果是这样,我哪里错了。