LocationListener与FusedLocationProviderClient均返回null无法获取定位
问题排查项
- 设备定位状态检查:确认设备全局定位开关已开启,且定位模式设置为高精度模式(同时启用GPS、Wi-Fi、移动网络定位)。若仅开启仅GPS模式,在室内等GPS信号弱的场景下无法获取定位,
getLastLocation()也会因为无历史定位缓存返回null。 getLastLocation局限性:该API仅返回系统缓存的历史定位,不会主动触发新定位请求。若设备近期没有其他应用触发过定位,缓存为空就会返回null。你代码中虽然同时调用了requestLocationUpdates,但新定位结果回调存在延迟,调用后立刻取缓存大概率为空。- 回调生命周期问题:你当前代码中的
LocationCallback是fetchLocation()方法内的局部变量,方法执行完成后若无外部引用持有,会被系统GC回收,导致定位结果返回时找不到回调对象,无法触发onLocationResult。 - 测试环境注意点:若使用模拟器测试,需要手动给模拟器注入定位坐标,默认模拟器无定位数据源,也会返回null。
- Google服务依赖检查:FusedLocationProvider依赖Google Play服务运行,需确认设备已安装可正常连接的Google Play服务。
修正方案(官方推荐FusedLocationProvider实现)
1. 先声明类成员变量,避免回调被回收
// 定位相关成员变量 private FusedLocationProviderClient fusedLocationProviderClient; private LocationRequest mLocationRequest; private LocationCallback mLocationCallback; private Location currentLocation; private static final int REQUEST_CODE = 1001;
2. 页面初始化时提前初始化定位回调
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 其他页面初始化逻辑 fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this); initLocationCallback(); fetchLocation(); }
3. 单独封装定位回调和配置逻辑
private void initLocationCallback() { mLocationCallback = new LocationCallback() { @Override public void onLocationResult(@NonNull LocationResult locationResult) { super.onLocationResult(locationResult); for (Location location : locationResult.getLocations()) { if (location != null) { currentLocation = location; // 拿到定位后直接调用Google Maps加标记逻辑即可 addMarkerToMap(currentLocation); // 若只需要单次定位,拿到结果后停止定位节省功耗 stopLocationUpdates(); } } } }; mLocationRequest = LocationRequest.create(); mLocationRequest.setInterval(10000); mLocationRequest.setFastestInterval(2000); mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); // 开启精准定位等待,设置30秒超时避免长时间无结果 mLocationRequest.setWaitForAccurateLocation(true); mLocationRequest.setExpirationDuration(30000); }
4. 修改定位请求逻辑,增加权限结果回调
private void fetchLocation() { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_CODE); return; } fusedLocationProviderClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.getMainLooper()); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == REQUEST_CODE) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { fetchLocation(); } } }
5. 增加停止定位逻辑,页面销毁时释放资源
private void stopLocationUpdates() { if (fusedLocationProviderClient != null && mLocationCallback != null) { fusedLocationProviderClient.removeLocationUpdates(mLocationCallback); } } @Override protected void onDestroy() { super.onDestroy(); stopLocationUpdates(); }
6. 确认清单文件权限配置(Android 12+需额外配置)
<!-- 基础定位权限 --> <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" /> <!-- Android 12+ 前台定位服务权限 --> <uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" /> <!-- 若需要后台定位需额外添加,且需要用户手动授权 --> <!-- <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" /> -->
内容的提问来源于stack exchange,提问作者Max Shady
相关产品推荐
相关产品推荐

