【发布时间】:2013-12-23 09:18:15
【问题描述】:
可能的重复,
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);
它总是将 isGPSEnabled 返回为 false。如果我手动打开 gps,它会返回 true。
任何人都可以告诉我这样可以打开 GPS 吗?如果是这样,我哪里出错了。
【问题讨论】:
-
问:我哪里出错了?答:知道问题是重复的,但还是继续发布。
标签: android gps locationmanager