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



基于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();
}

但是,当屏幕关闭一分钟时,相机预览会出现错误:在此处输入图像描述在此处输入图像描述

为什么以及如何避免?

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

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

最新更新