Android 工作室不检查 mFusedLocationProviderClient.getLastLocation().addOnSuccessListener 和 returns 当前位置 null

Android studio does not check mFusedLocationProviderClient.getLastLocation().addOnSuccessListener and returns current location null

我正在尝试从我的应用程序获取更新后的位置。但是我的应用程序不检查这段代码

public void onSuccess(Location 位置) {}mFusedLocationProviderClient.getLastLocation() .addOnSuccessListener(this, new OnSuccessListener(){} 和 returns 我的空位置。甚至我的设备位置已打开并且地图显示我的位置。我访问更新位置的代码如下:

  protected void onCreate(Bundle savedInstanceState) {
                mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
        }

          public void onMapReady(GoogleMap googleMap) {
                mMap = googleMap;

                Toast.makeText(this, "Map is ready", Toast.LENGTH_SHORT).show();
                if (mLocationPermissionGranted) {
                    getDeviceLocation();

                    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                            != PackageManager.PERMISSION_GRANTED &&
                            ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
                                    != PackageManager.PERMISSION_GRANTED) {
                        return;
                    }
                    mMap.setMyLocationEnabled(true);
                }
            }

        private void getDeviceLocation(){
                    try {
                        if(mLocationPermissionGranted) //true in my case
    {


                            mFusedLocationProviderClient.getLastLocation()
                                    .addOnSuccessListener(this, new OnSuccessListener<Location>() {

                                        @Override
                                        public void onSuccess(Location location) {
                                            // Got last known location. In some rare situations this can be null.
                                            if (location != null) {

                                                moveCamera(new LatLng(location.getLatitude(),location.getLongitude()),17);
                                               // mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(),location.getLongitude()),12),2000,null);

                                            }
                                            else{
                                                Toast.makeText(MapsActivity.this, "Unable to get current location", Toast.LENGTH_SHORT).show();
                                            }
                                        }

                                    });

                        }
                    }catch (SecurityException ex){

                    }
                }

我在堆栈跟踪中收到以下异常

09-11 15:12:14.525 5864-5864/? E/Zygote: v2
09-11 15:12:14.530 5864-5864/? E/Zygote: accessInfo : 0
09-11 15:12:27.333 5864-5864/com.example.muzammil.maptestingproject E/BoostFramework: BoostFramework() : Exception_1 = java.lang.ClassNotFoundException: Didn't find class "com.qualcomm.qti.Performance" on path: DexPathList[[],nativeLibraryDirectories=[/system/lib, /vendor/lib]]
09-11 15:12:39.327 5864-5864/com.example.muzammil.maptestingproject E/art: The String#value field is not present on Android versions >= 6.0

其他一切都是正确的,包括清单权限gradle依赖关系。可能有任何机构可以帮助我解决我的问题。提前致谢。

Fused Location Provider 将在至少有一个客户端连接到它时提供位置。如果客户端连接,它将立即尝试获取位置。如果您的 activity 是第一个连接的客户端并且您调用 getLastLocation() 可能没有足够的时间让第一个位置进入。

getLastLocation() 适用于立即需要某个位置或完全可以使用 none 的地方。如果你真的想等待一个位置,最好使用 requestLocationUpdates() 并等待回调而不是在线程中忙着等待。

这是使用requestLocationUpdates()

获得location的方法
    LocationCallback locationCallback;
    LocationRequest locationRequest;
    FusedLocationProviderClient fusedLocationClient;

    void getLocation() {
        locationRequest = LocationRequest.create()
                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                .setInterval(30 * 1000)
                .setFastestInterval(5 * 1000);

        locationCallback = new LocationCallback() {
            @Override
            public void onLocationResult(LocationResult locationResult) {
                super.onLocationResult(locationResult);
                //Location received 
            }
        };

        fusedLocationClient = LocationServices.getFusedLocationProviderClient(getApplicationContext());
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, null);
    }

并且不要忘记删除在 onStoponDestroy

中更新的位置
fusedLocationClient?.removeLocationUpdates(locationCallback)