为什么 ActivityCompat.requestPermissions 不提示权限请求?

问题描述

我正在尝试使用下面的代码向位置相关权限发出活动请求,但是当我运行代码时,由于某种原因,它没有请求用户授予权限。我在清单文件添加了粗略位置和精细位置的权限。当我打开活动时,它不会要求用户启用与位置相关的权限。我不确定为什么会这样,如果您能提供帮助,我将不胜感激。

    final int COARSE_LOCATION = 99,FINE_LOCATION = 100;
    ...
    @Override
        protected void onStart() {
            super.onStart();
            if (ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                // Todo: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions,and then overriding
                //   public void onRequestPermissionsResult(int requestCode,String[] permissions,//                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.ACCESS_FINE_LOCATION)) {
                    ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},FINE_LOCATION);
                } else {
                    ActivityCompat.requestPermissions(this,FINE_LOCATION);
                }
    
                if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.ACCESS_COARSE_LOCATION)) {
                    ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},COARSE_LOCATION);
                } else {
                    ActivityCompat.requestPermissions(this,COARSE_LOCATION);
                }
            }
        }

@Override
    public void onRequestPermissionsResult(int requestCode,@NonNull String[] permissions,@NonNull int[] grantResults) {
        switch (requestCode) {
            case FINE_LOCATION:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    getLastLocation();
                } else {
                    Toast.makeText(ContactListPage.this,"wrong permission",Toast.LENGTH_SHORT).show();
                }
        }
    }
 private void getLastLocation() {
if (ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // Todo: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions,and then overriding
            //   public void onRequestPermissionsResult(int requestCode,//                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        Task<Location> locationTask = fusedLocationProviderClient.getLastLocation();

        locationTask.addOnSuccessListener(new OnSuccessListener<Location>() {
            @Override
            public void onSuccess(Location location) {
                if(location != null){
                    latitude = location.getLatitude();
                    longitude = location.getLongitude();

                    Toast.makeText(ContactListPage.this,"Lat: " + latitude,Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(ContactListPage.this,"location was null",Toast.LENGTH_SHORT).show();
                }
            }
        });
        locationTask.addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(ContactListPage.this,"error getting last location",Toast.LENGTH_SHORT).show();
            }
        });
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)