Home > OS >  How to blink flashlight using "kotlin" on button click in android studio
How to blink flashlight using "kotlin" on button click in android studio

Time:06-25

I am an absolute beginner and just want to create an app in kotlin in which I just click a button and the flashlight of the phone blinks once please help this is urgent.

CodePudding user response:

Please check this answer. Also don't forgot to include camera permissions in manifest and request permission before turning on flash light

<uses-permission android:name="android.permission.FLASHLIGHT" />

https://stackoverflow.com/a/63259765/16098431

CodePudding user response:

  1. first add these two permissions to your manifest

.

   <uses-permission android:name="android.permission.CAMERA" />
   <uses-feature android:name="android.hardware.camera.flash" />
  1. Now use this function to switch the flash light status

.

private fun switchFlashLight(status: Boolean) {
        val cameraManager = getSystemService(Context.CAMERA_SERVICE) as CameraManager
        try {
            val cameraId = cameraManager.cameraIdList[0]

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                cameraManager.setTorchMode(cameraId, status)
            }
        } catch (e: CameraAccessException) {
            e.printStackTrace()
        }
    }
  • Related