当屏幕关闭时,背景中的相机预览会出错

问题描述 投票:0回答:1

基于GoogleSamples的代码,并在后台更改一些内容以便预览,而不是在示例代码中拍照。

例如:

     @Override
    public void onResume() {
        super.onResume();
        /*startBackgroundThread();

        // When the screen is turned off and turned back on, the SurfaceTexture is already
        // available, and "onSurfaceTextureAvailable" will not be called. In that case, we can open
        // a camera and start preview from here (otherwise, we wait until the surface is ready in
        // the SurfaceTextureListener).
        if (mTextureView.isAvailable()) {
            openCamera(mTextureView.getWidth(), mTextureView.getHeight());
        } else {
            mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
        }*/
    }

    @Override
    public void onPause() {
        /*closeCamera();
        stopBackgroundThread();*/
        super.onPause();
    }

   public synchronized void start() {
        if (mCameraDevice != null) {
            return;
        }
        startBackgroundThread();

        // When the screen is turned off and turned back on, the SurfaceTexture is already
        // available, and "onSurfaceTextureAvailable" will not be called. In that case, we can open
        // a camera and start preview from here (otherwise, we wait until the surface is ready in
        // the SurfaceTextureListener).
        if (mTextureView.isAvailable()) {
            openCamera(mTextureView.getWidth(), mTextureView.getHeight());
        } else {
            mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
        }
    }

    public synchronized void stop() {
        if (mCameraDevice == null) {
            return;
        }
        closeCamera();
        stopBackgroundThread();
    }

但是,如果屏幕关闭一分钟,相机预览就会出错:enter image description hereenter image description here

为什么以及如何避免呢?

android android-camera2
1个回答
0
投票

startForegroundService(new Intent(this,EmptyService.class));

前台服务需要int活动,然后一切正常。

© www.soinside.com 2019 - 2024. All rights reserved.