FusedLocationClient返回一些用户的空值

时间:2018-07-02 16:49:31

标签: android location

在我的Android应用中,我目前正在使用Fused Location Provider api(请参见下面的代码)来获取用户的位置,并且我对我的应用无法在应用中获取自己的位置的投诉很少。

    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(LOCATION_INTERVAL);
    mLocationRequest.setFastestInterval(1000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

    if ( ContextCompat.checkSelfPermission( this, android.Manifest.permission.ACCESS_FINE_LOCATION ) == PackageManager.PERMISSION_GRANTED ) {
        mLocationCallback = new LocationCallback() {
            @Override
            public void onLocationResult(LocationResult locationResult) {
                if (locationResult == null) {
                    return;
                }
                if(locationResult.getLastLocation() != null)
                    currentLocation = locationResult.getLastLocation();
            }
        };
        mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback,null);
        locationOn = true;
    }

经过一些检查,似乎有时它会在一段时间(或永远)内返回空值,而我的应用程序将无法正常工作。其他GPS应用程序也可以正常工作(例如Google Maps)。

我的实现是否正确,还是像Google Play服务这样的外部设备处于省电模式?

1 个答案:

答案 0 :(得分:0)

您所做的正确,但是位置提供程序有时返回null。您可能必须正确处理它。如果要手动获取位置,请使用LocationManager

尝试使用我的代码的这段代码(使用该代码段,我永远不会在我想要的位置获得null值,因为它至少会返回最后一个位置,或者正确向用户显示错误消息以及如何解决该错误消息。:

private void updateLocations(){

 String str_latitude = "";

 String str_longitude = "";

 String provider = "";

    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {

        provider = LocationManager.GPS_PROVIDER;

    } else if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {

        provider = LocationManager.NETWORK_PROVIDER;
    }

    if (!provider.equals("")) {

        mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(MainActivity.this);

        @SuppressLint("MissingPermission") Task locationResult = mFusedLocationProviderClient.getLastLocation();

        locationResult.addOnCompleteListener(MainActivity.this, new OnCompleteListener() {

            @Override
            public void onComplete(@NonNull Task task) {
                if (task.isSuccessful()) {

                   Location mLastKnownLocation = (Location) task.getResult();

                    if (mLastKnownLocation != null) {

                        str_longitude = String.valueOf(mLastKnownLocation.getLatitude());

                        str_latitude = String.valueOf(mLastKnownLocation.getLongitude());

                        Toast.makeText(context, "Lat: "+str_latitude + ", Long " + str_longitude, Toast.LENGTH_LONG).show();
                    }

                    if (str_latitude.equals("") || str_longitude.equals("")) {

                        Snackbar.make(coordinatorLayout, "Couldn't find location, please try again in a moment", Snackbar.LENGTH_LONG).show();
                    }
                }
            }
        });


    } else if (provider.equals("")) {

        Snackbar snackbar = Snackbar.make(coordinatorLayout, "Seems like all providers of device are disabled", Snackbar.LENGTH_LONG);

        snackbar.setAction("Enable", new View.OnClickListener() {

            @Override
            public void onClick(View v) {
            //Takes user to gps enable settings.
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);
            }
        });

        snackbar.show();
    }

}});