在运行时请求权限Android Marshmallow无效

时间:2016-07-08 10:17:43

标签: android android-permissions

我禁用了移动设备的位置,但它仍然显示已在吐司中授予的位置。

 @Override
public void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
    if(Build.VERSION.SDK_INT>=23){

        showRunTimePermission();
    }
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_search_v2, container,
            false);




    return view;
}

//如果禁用位置,则显示警告对话框。

private void showRunTimePermission() {
   if(ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION)!=PackageManager.PERMISSION_GRANTED ||
           ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)!=PackageManager.PERMISSION_GRANTED){
       requestLocationPermission();
   }else{
       Toast.makeText(getActivity(), "Location already granted", Toast.LENGTH_SHORT).show();
   }

}

private void requestLocationPermission() {
    if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),Manifest.permission.ACCESS_COARSE_LOCATION) || ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),Manifest.permission.ACCESS_FINE_LOCATION)){
        final AlertDialog locationDialog = new AlertDialog.Builder(getActivity())
                .setCancelable(false).create();
        View dialogView = View.inflate(getActivity(), R.layout.dialog_location_access, null);
        TextView okBtn= (TextView) dialogView.findViewById(R.id.dialog_location_ok_button);
        okBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityCompat
                        .requestPermissions(getActivity(), PERMISSIONS_LOCATION,
                                LOCATION_PERMISSIONS_REQUEST);
            }
        });
        TextView denyBtn= (TextView) dialogView.findViewById(R.id.dialog_location_deny_button);
        denyBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                locationDialog.dismiss();
            }
        });

        locationDialog.setView(dialogView);

        locationDialog.show();

    }else{
        ActivityCompat.requestPermissions(getActivity(), PERMISSIONS_LOCATION,
                LOCATION_PERMISSIONS_REQUEST);
    }
}

//如果位置打开或关闭,吐司将相应显示。

    @Override
public void onRequestPermissionsResult(int requestCode, String[]        permissions, int[] grantResults) {
    if (requestCode==LOCATION_PERMISSIONS_REQUEST){
        if (grantResults.length == 1 &&
                grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(getActivity(), "Location granted", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getActivity(), "Location permission denied", Toast.LENGTH_SHORT).show();
        }
    }else {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

}

2 个答案:

答案 0 :(得分:0)

启用位置和授予位置权限是两回事。要检查位置是否已启用:How to check if Location Services are enabled?

答案 1 :(得分:0)

那么,在获得许可之前,您需要检查是否授予了许可,然后您必须征得许可!

试试这个Library这可以帮助您自动提示您只需要传递权限名称。