我是安卓新手。我正在尝试使用 GPS 获取我的位置。我已经包括了权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
每当我试图Location
从LocationManager
使用中得到我的东西时,我都会getLastKnownLocation
得到空对象。此外,GPS 已启用。这是我的代码:
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
Location location;
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Log.d("ManualLog", "GPS_DISABLED");
Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(settingsIntent);
} else {
Log.d("ManualLog", "GPS_ENABLED");
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}