原创

フラッシュライトのみを使用する場合、カメラの許可を求める必要がありますか?

温馨提示:
本文最后更新于 2024年04月12日,已超过 47 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

タイトルであるように、FLASHLIGHT 権限を使用しただけです。これが私の FLASHLIGHT コードです

import android.annotation.SuppressLint
import android.content.Context
import android.hardware.camera2.CameraManager
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch

class FlashlightHelper(
    var context: Context
) {
    private val manager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
    private var cameraId: String? = null
    private var job: Job? = null

companion object {
    @SuppressLint("StaticFieldLeak")
    private var instance: FlashlightHelper? = null

    fun getInstance(
        context: Context
    ): FlashlightHelper {
        if (instance == null) {
            instance = FlashlightHelper(context)
        }
        return instance!!
    }
}

init {
    kotlin.runCatching {
        cameraId = manager.cameraIdList[0] ?: "0"
    }
}

fun toggleSOS(
    enable: Boolean,
    timeOn: Int = 300,
    timeOff: Int = 100,
    numberOfFlashes: Int = -1,
    callback: ((Boolean) -> Unit)? = null
) {
    job?.cancel()
    if (enable) {
        job = CoroutineScope(Dispatchers.Default).launch {
            if (numberOfFlashes <= 0) {
                while (isActive) {
                    toggleFlashlight(true)
                    delay(timeOn.toLong())

                    toggleFlashlight(false)
                    delay(timeOff.toLong())
                }
            } else {
                repeat(numberOfFlashes) {
                    toggleFlashlight(true)
                    delay(timeOn.toLong())

                    toggleFlashlight(false)
                    delay(timeOff.toLong())
                }
            }
            callback?.let { it(true) }
        }
    } else {
        job?.cancel()
        toggleFlashlight(false)
    }

}

fun toggleFlashlight(enable: Boolean) {
    kotlin.runCatching {
        cameraId?.let {
            manager.setTorchMode(it, enable)
        }
    }
}

Flash のオンとオフをその間だけで、その間カメラの許可を求めるアプリがたくさんあるようです。許可を得る必要がありますか?許可を求めなくても、オンまたはオフを許可できるからです。

Flash のオンとオフをその間だけで、その間カメラの許可を求めるアプリがたくさんあるようです。許可を得る必要がありますか?許可を求めなくても、オンまたはオフを許可できるからです。

正文到此结束
热门推荐
本文目录