Android:Activity/Fragment中申请运行时权限(onRequestPermissionsResult收不到数据)

一:actvity中获取零时权限

 String[] permissions = new String[]{
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
    };
 // 声明一个集合,在后面的代码中用来存储用户拒绝授权的权
 List mPermissionList = new ArrayList<>();




/**
     * 友盟分享
     * @author lucas
     * created at 2019/4/17 10:55 AM
     */

    //请求图片存储本地的权限
    public void askpermisson(){
        /**
         * 获取零时权限
         */
        mPermissionList.clear();
        for (int i = 0; i < permissions.length; i++) {
            if (ContextCompat.checkSelfPermission(BigeventSituationActivity.this, permissions[i]) != PackageManager.PERMISSION_GRANTED) {
                mPermissionList.add(permissions[i]);
            }
        }
        if (mPermissionList.isEmpty()) {
            //未授予的权限为空,表示都授予了
            LogUtils.w("onCreate: 已经授权!!!!");

            show_share_pic();//展示要分享的图片
            share();


        } else {//请求权限方法
            String[] permissions = mPermissionList.toArray(new String[mPermissionList.size()]);//将List转为数组
            ActivityCompat.requestPermissions(BigeventSituationActivity.this, permissions, 9);
        }
    }







 @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

        if (requestCode == 9) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                show_share_pic();//展示要分享的图片
                share();
            } else {
                Log.e(TAG, "onRequestPermissionsResult: 申请权限已拒绝");
            }
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }


二:fragment中获取临时权限

    String[] permissions = new String[]{
//            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
    };
    // 声明一个集合,在后面的代码中用来存储用户拒绝授权的权
    List mPermissionList = new ArrayList<>();






    /**
     * 友盟分享
     * @author lucas
     * created at 2019/4/17 10:55 AM
     */

    //请求图片存储本地的权限
    public void askpermisson(int pos){
        /**
         * 获取零时权限
         */
        mPermissionList.clear();
        for (int i = 0; i < permissions.length; i++) {
            if (ContextCompat.checkSelfPermission(getActivity(), permissions[i]) != PackageManager.PERMISSION_GRANTED) {
                mPermissionList.add(permissions[i]);
            }
        }
        if (mPermissionList.isEmpty()) {
            //未授予的权限为空,表示都授予了
            LogUtils.w("onCreate: 已经授权!!!!");

            show_share_pic(pos);//展示要分享的图片
            share();


        } else {//请求权限方法
            String[] permissions = mPermissionList.toArray(new String[mPermissionList.size()]);//将List转为数组
            BigeventFragment.this.requestPermissions( permissions, 9);
        }
    }




 @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

        Log.e(TAG, "onRequestPermissionsResult: 电放费  请求权限的返回!!!!" );
        if (requestCode == 9) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                show_share_pic(share_pic_position);//展示要分享的图片
                share();
            } else {
                Log.e(TAG, "onRequestPermissionsResult: 申请权限已拒绝");
            }
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

三:注意点总结:

Fragment中申请运行时权限的时候,使用Fragment本身来进行申请

//在Activity中使用的时候我们的代码如下:

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
        1);
//我们在Fragment中申请时不是使用AcyivityCompat而是使用Fragment本身,代码如下:

HomeFragment.this.requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
        1);

 

你可能感兴趣的:(Android:Activity/Fragment中申请运行时权限(onRequestPermissionsResult收不到数据))