我正在开发一个应用程序,因为我需要找到当前位置值。为此,我以编程方式启用了 gps。现在,我关闭并打开我的手机。现在,我无法获取当前位置值,但我设备的 gps 已打开。
我在(motorola 2.3.3、htc 2.3.4 和 nexus 4.2.1)中发现了这个问题
我使用了以下代码。
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);
locationManager = (LocationManager) getSystemService (Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
List<String> provider = locationManager.getProviders(true);
Location location = null;
for (int i=provider.size()-1; i>=0; i--)
{
location = locationManager.getLastKnownLocation(provider.get(i));
if (location != null) break;
if (location != null)
{
String latitude=String.valueOf(location.getLatitude());
String longitude=String.valueOf(location.getLongitude());
}