Android onRequestPermissionsResult没有处理我的片段

时间:2017-05-03 08:56:13

标签: android android-permissions

在我的片段中,我的活动页面之一,我想获得用户的许可并在授予权限后执行某些操作,但onRequestPermissionsResult无法正常工作,并且在授予后我看不到任何日志许可

public class FragmentAboutUs extends Fragment implements ActivityCompat.OnRequestPermissionsResultCallback {
    private Activity          activity;
    private Context           context;
    ...
    private int WRITE_EXTERNAL_STORAGE = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override

    public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_about_us, container, false);

        activity = getActivity();
        context = getActivity().getBaseContext();

        ...

        aboutUs = SQLite.select().from(AboutUs.class).querySingle();
        if (aboutUs == null) {
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                    ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, WRITE_EXTERNAL_STORAGE);
                } else {
                    ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, WRITE_EXTERNAL_STORAGE);
                }
            }
        }
        return view;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == WRITE_EXTERNAL_STORAGE) {
            Log.e("permission granted"," OK");
        } else {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

删除它:

implements ActivityCompat.OnRequestPermissionsResultCallback

片段本身可以处理运行时权限OnRequestPermissionsResultCallback

使用它像:

 final int MyVersion = Build.VERSION.SDK_INT;
if (MyVersion > Build.VERSION_CODES.LOLLIPOP_MR1) {
        if (!checkIfAlreadyhavePermission()) {
            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE }, WRITE_EXTERNAL_STORAGE);  //request permission
        } else {
            //call your method permision already granted
        }
    } else {
            //call your method permission not needed for this version
    }


private boolean checkIfAlreadyhavePermission() {
        int result = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
        return result == PackageManager.PERMISSION_GRANTED;
    }

现在,您可以在public void onRequestPermissionsResult()

中的Fragment获取权限结果

使用:

public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case WRITE_EXTERNAL_STORAGE: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                Toast.makeText(getActivity(), "permission granted", Toast.LENGTH_LONG).show();
                //call your action

            } else {
                Toast.makeText(getActivity(), "permission denied", Toast.LENGTH_LONG).show();
            }
            break;
        }
    }
}

答案 1 :(得分:0)

使用:

requestPermissions(permissions, PERMISSIONS_CODE);

取而代之的是:

ActivityCompat.requestPermissions()

您可以在这里阅读更多内容: Answer