如何阻止 mLastKnownLocation 在应用程序首次加载到模拟器时返回 null
How to stop mLastKnownLocation from returning null on app's first load on emulator
我正在使用模拟器测试我的应用程序。
如果我擦除模拟器上的所有数据并启动应用程序,然后使用 android 开发人员文档中的以下方法:
getLocationPermission();
updateLocationUI();
getDeviceLocation();
抛出错误,因为在第一次使用应用程序时 mLastKnownLocation 为空。
我已经阅读了很多关于此的帖子,问题似乎是在从未使用过它的定位服务的设备上打开应用程序似乎是问题所在,因为从未记录过最后一个位置。
这一切都很好,但我正在努力实施一个简单的解决方案。有一些关于 mGoogleApiClient
的帖子
GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */,
this /* OnConnectionFailedListener */)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.build();
但不确定如何解决问题。
我想知道在 getLocationPermission 为 true 之后,是否有一种简单的方法可以强制打开定位服务并获取设备位置?
谢谢。
为简单起见,请尝试使用此库 https://github.com/mrmans0n/smart-location-lib。一次即可领取。
如果你想获取一次位置,你只需输入此代码
SmartLocation.with(context).location()
.oneFix()
.start(new OnLocationUpdatedListener() { ... });
我正在使用模拟器测试我的应用程序。 如果我擦除模拟器上的所有数据并启动应用程序,然后使用 android 开发人员文档中的以下方法:
getLocationPermission();
updateLocationUI();
getDeviceLocation();
抛出错误,因为在第一次使用应用程序时 mLastKnownLocation 为空。
我已经阅读了很多关于此的帖子,问题似乎是在从未使用过它的定位服务的设备上打开应用程序似乎是问题所在,因为从未记录过最后一个位置。
这一切都很好,但我正在努力实施一个简单的解决方案。有一些关于 mGoogleApiClient
的帖子GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */,
this /* OnConnectionFailedListener */)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.build();
但不确定如何解决问题。
我想知道在 getLocationPermission 为 true 之后,是否有一种简单的方法可以强制打开定位服务并获取设备位置?
谢谢。
为简单起见,请尝试使用此库 https://github.com/mrmans0n/smart-location-lib。一次即可领取。
如果你想获取一次位置,你只需输入此代码
SmartLocation.with(context).location()
.oneFix()
.start(new OnLocationUpdatedListener() { ... });