我正在使用简单的 位置管理器 对象来获取设备的 lastKnownLocation()但是在 return 中得到 null object 谁能告诉我为什么?
I am using simple location manager object to get lastKnownLocation() of device but getting null object in return can any one tell me why ?
代码:
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" />
所以有什么需要设置的吗?我已检查我的位置服务是否已在设备中打开请提供一些工作示例的链接
so is anything missing to set up? I have checked my location service is on in device please give some links for working examples
这个问题的答案很愚蠢...我重新启动了我的设备...它工作了...请确保您的位置服务已启用并正常工作正确的
Silly thing as answer to this ...I restarted my device ...and it worked...Please ensure that your service for location are enabled and working properly
这篇关于如何在 Android 中获取位置管理器的最后一个已知位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!