如何在片段中添加棉花糖权限?

how to add marshmallow permissions in fragment?

在我的项目中,我在添加权限后请求片段中的棉花糖权限 onrequestpermissiomResult is not calling in my fragment 我的代码:

public class  @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    profileView = inflater.inflate(R.layout.fragment_profile, container, false);
    server_utilities = new ServerUtilities();
    utilities = new Utilities(getActivity());
    getUserProfileInfo();
    callbackManager = CallbackManager.Factory.create(); 

cameraImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ***if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                //First checking if the app is already having the permission
                if (utilities.isReadStorageAllowed(getActivity())) {
                    //If permission is already having then showing the toast
                    Toast.makeText(ctx.getApplicationContext(), "You already have the permission", Toast.LENGTH_LONG).show();
                    //Existing the method with return
                    showFourButtonsBSDialog();
                } else {
                    //If the app has not the permission then ask for the permission
                    utilities.checkPermissions(ctx, getActivity());
                }
            } else
                showFourButtonsBSDialog();***
        }
    });

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {
    switch (requestCode) {
        case REQUEST_PICKIMAGE:
            //If permission is granted
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                //Displaying a toast
                Toast.makeText(MainActivity.this, "Permission granted now you can read the storage", Toast.LENGTH_LONG).show();
                profile_fragment.showFourButtonsBSDialog(); //todo
            } else {
                //Displaying another toast if permission is not granted
                Toast.makeText(MainActivity.this, "Oops you just denied the permission", Toast.LENGTH_LONG).show();
            }
    }
}

如何在 activity 的片段中添加棉花糖权限?

此代码适用于所有 API 级别 >= 23

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    if (getContext().checkSelfPermission(
                            Manifest.permission.READ_EXTERNAL_STORAGE)
                            != PackageManager.PERMISSION_GRANTED) {
                        requestPermissions(
                                new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                                FINE_GALLERY_PERMISSION_REQUEST);
                    } else {
                        galleryIntent();
                    }
                }
                else{
                    galleryIntent();
                }

并且在你的方法中不要忘记添加

startActivityForResult(yourIntent, YOUR_REQUEST_CODE);

因为你没有post你在获得许可后调用的方法我无法判断你是否发送了正确的请求代码,无论如何检查请求代码是否相同,并且不要忘记添加权限在清单中!

在谷歌搜索一小时后,将其添加到我们的父 activity

  private void replaceFragmentInternal(int contentFrameId, Fragment replacingFragment) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction()
            .replace(contentFrameId, replacingFragment)
            .commit();
}

会在相关片段中自动调用onrequest权限结果 作为参考,请看看这个