【问题标题】:FusedLocation not working when I turn off and on the Location in google settings android当我在谷歌设置android中关闭和打开位置时,FusedLocation 不起作用
【发布时间】:2018-09-08 01:15:57
【问题描述】:

我通过将当前位置与响应中的给定位置进行比较来使用融合位置。

一切正常。但是当我在设置中关闭位置然后打开位置时,以下方法不起作用。它直接属于其他部分,在 toast 消息中显示为 location : null .. 只有当我卸载应用程序然后安装时,它才能工作。我无法弄清楚为什么在谷歌设置中关闭并打开位置后融合位置不起作用。

在 onCreate 方法中

call_permissions();

private void call_permissions() {
        int result;
        List<String> listPermissionsNeeded = new ArrayList<>();
        for (String p : permissions) {
            result = ContextCompat.checkSelfPermission(getApplicationContext(), p);
            if (result != PackageManager.PERMISSION_GRANTED) {
                listPermissionsNeeded.add(p);
            }
        }
        if (!listPermissionsNeeded.isEmpty()) {
            ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray
                    (new String[listPermissionsNeeded.size()]), MULTIPLE_PERMISSIONS);
        } else {

            getLocation(); //here using getLocation.
            //    splash_timer();
        }
        return;
    }



public static final int MULTIPLE_PERMISSIONS = 10;
    String[] permissions = new String[]{
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA,
            Manifest.permission.ACCESS_FINE_LOCATION};


void getLocation() {
        if (ContextCompat.checkSelfPermission(EducationAndFeedbackActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            FusedLocationProviderClient mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

            mFusedLocationClient.getLastLocation()
                    .addOnCompleteListener(this, new OnCompleteListener<Location>() {
                        @Override
                        public void onComplete(@NonNull Task<Location> task) {
                            if (task.isSuccessful() && task.getResult() != null) {
                                Location mLastLocation = task.getResult();
                                if (AppUtils.isMockLocationEnabled(mLastLocation, EducationAndFeedbackActivity.this)) {
                                    //Toast.makeText(EducationAndFeedbackActivity.this, "Matikan Fake Gps", Toast.LENGTH_SHORT).show();
                                    final Dialog dialog = new Dialog(EducationAndFeedbackActivity.this);
                                    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                                    dialog.setContentView(R.layout.custom_alert_response_accepted1);

                                    Button ok = (Button) dialog.findViewById(R.id.ok_button);
                                    TextView textView = (TextView) dialog.findViewById(R.id.response);
                                    textView.setText("Matikan Fake Gps!\nMohon matikan lokasi tiruan di setting pengembang");

                                    Button cancel = (Button) dialog.findViewById(R.id.Cancel_button);

                                    dialog.setCancelable(false);
                                    dialog.show();

                                    ok.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View view) {
                                            startActivity(new Intent(android.provider.Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS));
                                            dialog.dismiss();
                                            finish();
                                        }
                                    });
                                    cancel.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View view) {
                                            dialog.dismiss();
                                            finish();
                                        }
                                    });
                                    return;
                                } else {
                                    Log.e("Current lat", "Current lat" + mLastLocation.getLatitude());
                                    Log.e("Current long", "Current long" + mLastLocation.getLongitude());
                                    Log.e("retailer lat", latitude);
                                    Log.e("retailer long", longitude);
                                    double rangess = AppUtils.distance(Double.parseDouble(latitude), Double.parseDouble(longitude) , mLastLocation.getLatitude(), mLastLocation.getLongitude());
                                    Log.e("ranges", "rangess" + rangess);
                                    Log.e("feedback range", "feedback range" + rangess);
                                    if(feedbackrange!=null) {
                                        if (rangess <= Double.parseDouble(feedbackrange)) {
                                            Log.e("Range is less", "Range is less"); //nothing to do.
                                        } else {
                                            showDialogAlert(EducationAndFeedbackActivity.this, getString(R.string.education_feedback_alert));
                                        }
                                    }
                                }

                            } /*else {
                                Toast.makeText(getApplicationContext(), "Location null", Toast.LENGTH_LONG).show();

                            }*/
                        }
                    });
        }
    }

在上述方法中,它在从谷歌设置中关闭和打开位置后显示位置空。 :(

 @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MULTIPLE_PERMISSIONS: {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // permissions granted.
                    Toast.makeText(this, "Permission Granted, Now you can Location access.", Toast.LENGTH_LONG).show();
                    getLocation();
                    //  splash_timer();
                } /*else {
                   *//* Toast.makeText(this, "Permission Denied, Now you cannot Location access.", Toast.LENGTH_LONG).show();
                    finish();*//*
                    // no permissions granted.
                }*/
                return;
            }

        }
    }

【问题讨论】:

  • 关于这个错误的任何更新?

标签: android android-location android-maps-v2 fusedlocationproviderapi android-fusedlocation


【解决方案1】:

根据the documentation

location对象在以下情况下可能为null:

设备设置中的位置信息已关闭。即使先前检索到最后一个位置,结果也可能为空,因为禁用位置也会清除缓存。

设备从未记录其位置,可能是新设备或已恢复出厂设置的设备。

设备上的 Google Play 服务已重新启动,并且在服务重新启动后没有活动的 Fused Location Provider 客户端请求位置。为避免这种情况,您可以创建一个新客户端并自己请求位置更新。有关详细信息,请参阅接收位置更新。

第一点描述了您当前的情况,因为关闭位置会清除缓存,因此getLastLocation() 返回 null,因为没有存储以前已知的位置。

如果您当前的 Play 服务版本支持,您可以改用 getCurrentLocation(...),或者一次性使用 requestLocationUpdates(...) 来获取实时位置。

【讨论】:

    【解决方案2】:

    正如 Droid 所说,禁用和重新启用设备位置会清除缓存,这意味着 getLastLocation() 返回 null。

    我通过拨打getCurrentLocation()解决了这个问题

    然而,您似乎必须使用LocationRequest.PRIORITY_HIGH_ACCURACY,否则即使getCurrentLocation() 返回null。这适用于我的 Oneplus6t,仅授予 ACCESS_COARSE_LOCATION。但是,对于其他设备(包括像素),也需要 ACCESS_FINE_LOCATION 权限。

    文档没有提到这两件事,但我猜设备计算的第一个位置必须相当精确,这意味着需要高精度优先级。稍后的请求可能不太精确,因为设备已经知道您在世界的哪个位置。

    【讨论】:

      猜你喜欢
      • 2016-10-11
      • 2017-02-24
      • 1970-01-01
      • 2018-09-04
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多