Before the operation is performed automatically obtain the appropriate permissions: realization

At first, I wrote that, click on the pop-up permissions required, the permit will also need to click once again I want to perform the operation, optimization tests for this purpose proposed, should be "after obtaining permission" will continue the previous operation, and not a user operation once again, in order to record it.

How we are allowed to know, or refused to power? This time we need onRequestPermissionsResult or onActivityResult method.

 

Pit: Get permission fragment inside, does not perform fragment in onRequestPermissionsResult

Resolution: The following code fragment in which the main activity:

  // Fragment里申请权限不回调onRequestPermissionsResult
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        // 获取到Activity下的Fragment
        List<Fragment> fragments = getSupportFragmentManager().getFragments();
        if (fragments == null) {
            return;
        }
        // 查找在Fragment中onRequestPermissionsResult方法并调用
        for (Fragment fragment : fragments) {
            if (fragment != null) {
                // 这里就会调用我们Fragment中的onRequestPermissionsResult方法
                fragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
            }
        }
    }

 

1. After obtaining permission in activity / fragment inside, automatic permission before continuing, eg: Camera Permissions

 //点击按钮时判断是否有相机权限,没有就申请(允许后执行ZM()不需要再次点击)
if (Build.VERSION.SDK_INT >= 23) {//低于23也需要判断相机是否开启,先动态申请
      int checkPermission = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA);
      if (checkPermission != PackageManager.PERMISSION_GRANTED) {
           ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, 22);//打开相机权限,requestCode=22,后面用到
           } else {
                ZM();//有相机权限时,执行ZM方法
           }


 @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case 22://对应前面的22
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {//会报数组越界,要判断好grantResults[]
                    ZM();//有相机权限时,执行ZM方法
                } else {
                   //没有允许
                }
                break;
        }
    }

2. Get used when GPS, Bluetooth is onActivityResult

Pit: get inside the fragment, onActivityResult need to write the main activity inside

2.1 does not have permission to apply for open GPS / Bluetooth

  //获取GPS
  Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  activity.startActivityForResult(intent, 2020);

 //获取蓝牙
 Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
 activity.startActivityForResult(intent, 2022);

 2.2 onActivityResult must be the activity inside, which need to open the case fragment GPS / Bluetooth, you can write LiveDataBus in the "success" which continue to operate after receiving a fragment

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 2020 || requestCode == 2022) {
            if (resultCode == RESULT_OK) {//RESULT_OK 成功,RESULT_CANCELED 取消,
                //允许之后的操作,
            }
        }
    }

 

Published 69 original articles · won praise 11 · views 40000 +

Guess you like

Origin blog.csdn.net/weixin_40420578/article/details/104427334