【发布时间】:2014-10-18 10:51:43
【问题描述】:
我正在使用简单的 位置管理器 对象来获取设备的 lastKnownLocation() 但是在 return 中获取 null object 谁能告诉我为什么?
代码:
public Location getLocation() {
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
if (locationManager != null) {
Location lastKnownLocationGPS = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (lastKnownLocationGPS != null) {
return lastKnownLocationGPS;
} else {
Location loc = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
System.out.println("1::"+loc);----getting null over here
System.out.println("2::"+loc.getLatitude());
return loc;
}
} else {
return null;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.location);
getLocation();-----calling service
}
已授予权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
所以有什么需要设置的吗?我已检查我的位置服务是否已在设备中开启 请提供一些工作示例的链接
【问题讨论】:
-
@deniz thnks 我正在实现你给出的第一个链接。我得到 lastLocation 的空值这里是代码 if (mUpdatesRequested) { mLocationClient.requestLocationUpdates(mLocationRequest, this); }
-
不使用启用的“使用无线网络”和“位置和 Google 搜索”
-
您在开发者文档中尝试过该示例吗?希望对你有帮助
标签: android geolocation locationmanager