【问题标题】:FusedLocationClient returning null values for some few usersFusedLocationClient 为一些用户返回空值
【发布时间】:2018-12-10 22:34:34
【问题描述】:

在我的 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 地图)。

我的实现是正确的,还是像 Google Play Services 这样的外部设备被置于省电模式?

【问题讨论】:

    标签: android location


    【解决方案1】:

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

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

    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();
        }
    
    }});
    

    【讨论】:

    • 我正在测试您的 sn-p,到目前为止效果很好。很难知道它是否足够一致,因为我在 2 个月内有超过 2k 的用户,到目前为止只收到了 1 起投诉。我仍然没有深入解释为什么会发生我所描述的问题
    • @arvere 我自己在 2 个项目中使用过这段代码,因为我得到了空位置和应用程序崩溃。在互联网上搜索了很长时间后,我将多种解决方案合并为一个,以提供强大的定位服务。希望它能如您所愿,如果您发现任何问题,请告诉我,如果您决定使用该代码,或者您认为代码对其他人有用,请将答案标记为解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-09
    • 2020-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    相关资源
    最近更新 更多