【问题标题】:App is blocked when opening from lock screen with face id使用人脸 ID 从锁定屏幕打开时应用程序被阻止
【发布时间】:2021-12-27 07:02:41
【问题描述】:

我有一个应用程序在我的应用程序运行时禁止其他应用程序使用相机,但是当我将其关闭并重新打开时,我的设备具有人脸 ID,因此我的应用程序被阻止,就像其他应用程序使用它时一样。使用相机。这是我的相机检查代码:

fun checkCameraAvailable(listener: ((isAvailable: Boolean) -> Unit)) {

    val manager = getSystemService(CAMERA_SERVICE) as CameraManager
    val handler = Handler(Looper.getMainLooper())

    manager.registerAvailabilityCallback(object : AvailabilityCallback() {
        override fun onCameraAvailable(cameraId: String) {

            listener.invoke(true)
            super.onCameraAvailable(cameraId)
        }

        override fun onCameraUnavailable(cameraId: String) {

            listener.invoke(false)
            super.onCameraUnavailable(cameraId)
        }
    }, handler)
}

【问题讨论】:

  • 分享你的崩溃

标签: android kotlin android-camera mdm lockscreen


【解决方案1】:

您的原始问题中提供的信息很少,但有效的问题之一可能是当您的应用在设备锁定后进入后台时,您没有正确停止相机预览。

当你不再需要它时,你应该stop the preview and release the camera

我将引用该链接中提到的文档:

使用相机完成您的应用程序后,就该进行清理了。特别是,您必须释放 Camera 对象,否则您可能会导致其他应用程序崩溃,包括您自己的应用程序的新实例。

您应该何时停止预览并释放相机?好吧,破坏预览表面是一个很好的提示,表明是时候停止预览并释放相机了,如 Preview 类中的这些方法所示。

override fun surfaceDestroyed(holder: SurfaceHolder) {
    // Surface will be destroyed when we return, so stop the preview.
    // Call stopPreview() to stop updating the preview surface.
    mCamera?.stopPreview()
}

/**
 * When this function returns, mCamera will be null.
 */
private fun stopPreviewAndFreeCamera() {
    mCamera?.apply {
        // Call stopPreview() to stop updating the preview surface.
        stopPreview()

        // Important: Call release() to release the camera for use by other
        // applications. Applications should release the camera immediately
        // during onPause() and re-open() it during onResume()).
        release()

        mCamera = null
    }
}

如果您使用的是 CameraX,它应该绑定到正在使用它的 Activity/Fragment 的生命周期,从而自动释放。

See an example here.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 2020-05-06
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多