简体   繁体   English

为什么当我第二次运行权限请求对话框时,我的应用程序没有显示位置按钮?

[英]Why my app doesn't show location button when I accept the permission request dialog the second time is runned?

I am programming an Android App in Java. 我正在用Java编写一个Android应用程序。 The activity holds a Google map and what I want is to have a button location at the top, like is shown in the picture: 该活动保存有一张Google地图,我想要的是在顶部有一个按钮位置,如图所示: 在此处输入图片说明

I want that when I click the location to point to my current Location and for that I am using the class FusedLocationProviderClient . 我希望当我单击该位置以指向我的当前位置时,为此,我正在使用类FusedLocationProviderClient

When I run the program everything goes fine until i follow these steps sequencially: 1- Run app first time and I deny permissions 2- Run app second time. 当我运行程序时,一切正常,直到我依次执行以下步骤:1-第一次运行应用程序,并且拒绝权限2-再次运行应用程序。 It request again permissions so I answer ok, but the button doesn't appear. 它再次请求权限,所以我回答确定,但没有出现该按钮。 3- I close the app, and because I said yes last time, to the location permission, the button is on the map and is working. 3-我关闭了该应用程序,并且由于我上次说了“是”,因此在获得位置许可后,该按钮在地图上并且可以使用。

Here is my code: 这是我的代码:

 private void checkPermissions(int fineLocationPermission) {
        if (fineLocationPermission != PackageManager.PERMISSION_GRANTED) { //If we don't have any permissions yet
            // TODO: Consider calling
            //We have to request permissions and in case the user refuse the permission we show an explanation of why he needs the permission
            //The following method returns true in case the user reject the permission
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                        PERMISSION_REQUEST_CODE);

        } else
            mLocationPermissionGranted=true;
    }

 @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_REQUEST_CODE:
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    //If the user accepts the dialog box then we get the last location and show button
                    mLocationPermissionGranted=true;

                } else {
                    mLocationPermissionGranted=false;
                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                    Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show();
                }
        }
    }

 public void onLocationChanged(final Location location) {
        String msg = "Updated location: " +
                Double.toString(location.getLatitude()) + ", " +
                Double.toString(location.getLongitude());
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        final LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());

        //Show button to locate current position

        mMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() {
            @Override
            public boolean onMyLocationButtonClick() {
                mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
                CameraUpdateFactory.newLatLngZoom(latLng,12);
                //mMap.setMaxZoomPreference(12);
                return false;
            }

        });

        // Add a marker our current position
        LatLng CurrentPosition = new LatLng(location.getLatitude(), location.getLongitude());
        mMap.addMarker(new MarkerOptions().position(CurrentPosition).title("Here you are!"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(CurrentPosition));
    }
 @Override
    public void onMapReady(GoogleMap googleMap) {
        //Create the map
        mMap = googleMap;

        //Set the type of the map: HYBRID, NORMAL, SATELLITE or TERRAIN. In our case TERRAIN type
        mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);

        //Set the zoom
        mMap.setMaxZoomPreference(12);

        //Set the markers
        MarkerOptions markerOptions= new MarkerOptions();

        //Checking permissions with the permissions we have included in the manifiest
        checkPermissions(permissionCheckFineLocation);



        if (mLocationPermissionGranted) {
            try {
                mMap.setMyLocationEnabled(true);
                //Last location task
                Task<Location> locationResult = mFusedLocationClient.getLastLocation();

                locationResult.addOnSuccessListener(this, new OnSuccessListener<Location>() {
                    @Override
                    public void onSuccess(Location location) {
                        //Got last known location. In some rare situations this can be null
                        if (location != null) {
                            //Logic to handle location object
                            onLocationChanged(location);
                        }
                    }
                });
            } catch (SecurityException e) {
                Log.e("error", e.getMessage());
            }
        }
}

I know is a weird question, but I don't know how to express myself. 我知道这是一个奇怪的问题,但我不知道该如何表达自己。 Any help would be great! 任何帮助将是巨大的! Thank you in advance! 先感谢您!

onMapReady isn't called (again) after permission is granted (this happens after onMapReady ). 再次授予权限后,不会再次调用onMapReady (这发生在onMapReady之后)。

Move the code in onMapReady in the if (mLocationPermissionGranted) clause to a separate method and call it from inside the if as well as from onRequestPermissionsResult if permission was granted. if (mLocationPermissionGranted)子句中onMapReady中的代码移动到单独的方法,然后从if内部以及从onRequestPermissionsResult如果已授予权限)中调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM