簡體   English   中英

為什么當我第二次運行權限請求對話框時,我的應用程序沒有顯示位置按鈕?

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

我正在用Java編寫一個Android應用程序。 該活動保存有一張Google地圖,我想要的是在頂部有一個按鈕位置,如圖所示: 在此處輸入圖片說明

我希望當我單擊該位置以指向我的當前位置時,為此,我正在使用類FusedLocationProviderClient

當我運行程序時,一切正常,直到我依次執行以下步驟:1-第一次運行應用程序,並且拒絕權限2-再次運行應用程序。 它再次請求權限,所以我回答確定,但沒有出現該按鈕。 3-我關閉了該應用程序,並且由於我上次說了“是”,因此在獲得位置許可后,該按鈕在地圖上並且可以使用。

這是我的代碼:

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

我知道這是一個奇怪的問題,但我不知道該如何表達自己。 任何幫助將是巨大的! 先感謝您!

再次授予權限后,不會再次調用onMapReady (這發生在onMapReady之后)。

if (mLocationPermissionGranted)子句中onMapReady中的代碼移動到單獨的方法,然后從if內部以及從onRequestPermissionsResult如果已授予權限)中調用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM