【问题标题】:Android FusedLocationProviderClient doesn't always workAndroid FusedLocationProviderClient 并不总是有效
【发布时间】:2018-12-25 14:35:25
【问题描述】:

我用三部手机测试了这段代码。它适用于其中两个,但不适用于第三个 Huawei y9 2019。有什么问题? , 为什么会显示 (?) 和 (*) 符号?

我的代码是:

mFusedLocationProviderClient = new FusedLocationProviderClient(this);
    mLocationRequest = LocationRequest.create();
    mLocationRequest.setInterval(1000);
    mLocationRequest.setFastestInterval(500);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_LOW_POWER);


    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        return;
    }

    mFusedLocationProviderClient.requestLocationUpdates(
            mLocationRequest,
            new LocationCallback() {
                @Override
                public void onLocationResult(LocationResult locationResult) {


                    if(locationResult != null){
                        Log.i("Your_Location",locationResult.getLastLocation().toString()+"\n\nrequestLocationUpdates() isCall");
                        textView.setText(locationResult.getLastLocation().toString());
                    }
                    else{
                        Log.i("Your_Location"," requestLocationUpdates() isCall Please Give Permission and On Location");
                        textView.setText("Please Give Permission and On Location");
                    }
                    super.onLocationResult(locationResult);


                }
            },
            getMainLooper()


    );

【问题讨论】:

  • 从技术上讲,输出符合Location 类的toString 契约——不同的实现会产生不同的结果。为什么不使用Location.convert 来获得精确的输出。
  • 但是,三星它显示出完美的价值,而不是 * 和 ?签名。

标签: android fusedlocationproviderclient


【解决方案1】:

我已经检查过华为以前的版本和其他手机,它对我来说工作正常。但是我没有Huawei y9 2019。所以你可以用这个代码检查。

//通过FusedLocationApi请求位置

public void registerRequestUpdate(final LocationListener listener) {
        mLocationRequest = LocationRequest.create();
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        localStorageInterval=sm.getLocalStorageInterval().get("localStorageInterval")*1000;
        mLocationRequest.setInterval(NOTIFY_INTERVAL/2); // every second


        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                try {
                    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, listener);
                } catch (SecurityException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                    if (!isGoogleApiClientConnected()) {
                        mGoogleApiClient.connect();
                    }

                    registerRequestUpdate(listener);
                 //   saveLocationAndSync();

                }
            }
        }, NOTIFY_INTERVAL);
    }

// 从此方法获取更新位置

 @Override
    public void onLocationChanged(Location location) {
        try {
              string lat = location.getLatitude(); 
              string long = location.getLongitude();

            }
        }catch (Exception ex){
            //exception
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-26
    • 1970-01-01
    • 1970-01-01
    • 2012-04-23
    • 2015-06-28
    • 2015-12-04
    相关资源
    最近更新 更多