代码之家  ›  专栏  ›  技术社区  ›  arvere

FusedLocationClient为一些用户返回空值

  •  0
  • arvere  · 技术社区  · 6 年前

    我很少看到我的应用程序在我的应用程序位置融合我的应用程序和我的应用程序目前无法得到他们的位置投诉。

        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应用程序也一样不错(比如谷歌地图)。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Aaditya Brahmbhatt    6 年前

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

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