每当在片段中打开应用程序时,每次在android中请求权限

时间:2018-07-31 14:07:09

标签: android android-fragments

Tab1Fragment.java

public class Tab1Fragment extends Fragment {
SurfaceView mCameraView;
CameraSource mCameraSource;
TextView mTextView;
private static final String TAG = "Tab1Fragment";
private static final int requestPermissionID = 101;

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

    mCameraView = view.findViewById(R.id.surfaceView);
     mTextView= view.findViewById(R.id.text_view);

    startCameraSource();
    return view;
}
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (requestCode != requestPermissionID) {
        Log.d(TAG, "Got unexpected permission result: " + requestCode);
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        return;
    }

    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        try {
            if (ActivityCompat.checkSelfPermission(getContext(),android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
            mCameraSource.start(mCameraView.getHolder());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    else
        System.runFinalizersOnExit(true);
}

private void startCameraSource() {

    //Create the TextRecognizer
    final TextRecognizer textRecognizer = new TextRecognizer.Builder(getActivity().getApplicationContext()).build();

    if (!textRecognizer.isOperational()) {
        Log.w(TAG, "Detector dependencies not loaded yet");
    } else {

        //Initialize camerasource to use high resolution and set Autofocus on.
        mCameraSource = new CameraSource.Builder(getActivity().getApplicationContext(), textRecognizer)
                .setFacing(CameraSource.CAMERA_FACING_BACK)
                .setRequestedPreviewSize(1280, 1024)
                .setAutoFocusEnabled(true)
                .setRequestedFps(2.0f)
                .build();

        /**
         * Add call back to SurfaceView and check if camera permission is granted.
         * If permission is granted we can start our cameraSource and pass it to surfaceView
         */
        mCameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                try {

                    if (ActivityCompat.checkSelfPermission(getActivity().getApplicationContext(),
                            Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {

                        ActivityCompat.requestPermissions(getActivity(),
                                new String[]{Manifest.permission.CAMERA},
                                requestPermissionID);
                        return;
                    }
                    mCameraSource.start(mCameraView.getHolder());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                mCameraSource.stop();
            }
        });

        //Set the TextRecognizer's Processor.
        textRecognizer.setProcessor(new Detector.Processor<TextBlock>() {
            @Override
            public void release() {
            }

            /**
             * Detect all the text from camera using TextBlock and the values into a stringBuilder
             * which will then be set to the textView.
             * */
            @Override
            public void receiveDetections(Detector.Detections<TextBlock> detections) {
                final SparseArray<TextBlock> items = detections.getDetectedItems();
                if (items.size() != 0 ){

                    mTextView.post(new Runnable() {
                        @Override
                        public void run() {
                            StringBuilder stringBuilder = new StringBuilder();
                            for(int i=0;i<items.size();i++){
                                TextBlock item = items.valueAt(i);
                                stringBuilder.append(item.getValue());
                                stringBuilder.append("\n");
                            }
                            mTextView.setText(stringBuilder.toString());
                        }
                    });
                }
            }
        });
    }
}
}

问题:

  

我正在使用这段代码中的片段,该应用仅在安装后一次请求许可,后来每次都未经许可打开。建议我进行更改,以使我的应用每次都可以请求许可来关闭如果用户拒绝该许可,则整个应用程序均无效。这是一个片段。

0 个答案:

没有答案