skip to Main Content
package com.example.flashlight

import android.content.Context
import android.hardware.camera2.CameraAccessException
import android.hardware.camera2.CameraManager
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    var flashLightStatus: Boolean = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        openFlashLight()

        while (true) {
            Handler(Looper.getMainLooper()).postDelayed({
                //Do something after 100ms
                closeFlashLight()
            }, 5000)
            Handler(Looper.getMainLooper()).postDelayed({
                //Do something after 100ms
                openFlashLight()
            }, 5000)
        }
    }

    private fun openFlashLight() {
        val cameraManager = getSystemService(Context.CAMERA_SERVICE) as CameraManager
        val cameraId = cameraManager.cameraIdList[0]
        if (!flashLightStatus) {
            try {
                cameraManager.setTorchMode(cameraId, true)
                flashLightStatus = true

            } catch (e: CameraAccessException) {
            }
        } else {
            try {
                cameraManager.setTorchMode(cameraId, false)
                flashLightStatus = false
            } catch (e: CameraAccessException) {
            }
        }
    }

    private fun closeFlashLight()
    {
        val cameraManager = getSystemService(Context.CAMERA_SERVICE) as CameraManager
        val cameraId = cameraManager.cameraIdList[0]
        if (flashLightStatus) {
            try {
                cameraManager.setTorchMode(cameraId, false)
                flashLightStatus = false

            } catch (e: CameraAccessException) {
            }
        } else {
            try {
                cameraManager.setTorchMode(cameraId, true)
                flashLightStatus = true
            } catch (e: CameraAccessException) {
            }
        }
    }
}

this now just open the flashlight on the phone and the flashlight stay open all the time because the while(true)

while (true) {
            Handler(Looper.getMainLooper()).postDelayed({
                //Do something after 100ms
                closeFlashLight()
            }, 5000)
            Handler(Looper.getMainLooper()).postDelayed({
                //Do something after 100ms
                openFlashLight()
            }, 5000)
        }

but if I’m removing the while true and doing only:

openFlashLight()
        Handler(Looper.getMainLooper()).postDelayed({
            //Do something after 100ms
            closeFlashLight()
        }, 5000)

this will open the flashlight and will wait 5 seconds then will close the flashlight.
but I want to make something endless that it will open the flash light wait 5 seconds close the flashlight wait 5 seconds open the flashlight wait 5 seconds close.

that way nonstop. open/close each 5 seconds nonstop. that’s why I tried to use the while(true) but then the flashlight just keep staying open nonstop.

2

Answers


  1. If you want to repeat the flashlight on/off cycles every 5 seconds you can do something like this:

    1. Keep a boolean flag as a class member:
    var flashLightOn = false
    
    1. Make a method that start the flashlight
    fun startFlashLight() {
        openFlashlight()
        flashLightOn = true
    
        val handler = Handler(Looper.getMainLooper())
        handler.postDelayed(object: Runnable() {
            override fun run() {
                if (flashLightOn) {
                    closeFlashLight()
                } else {
                    openFlashlight()
                }
                
                // Changing the toggle and calling the same Runnable again after 5 seconds
                flashLightOn = !flashLightOn
                handler.postDelayed(this, 5000)
            }
        }, 5000)
    }
    
    
    Login or Signup to reply.
  2. You can achieve this by using kotlin coroutines

      val scope = CoroutineScope(Dispatchers.Main)
    
        scope.launch {
            while (true) {
                openFlashlight()
                delay(5000)
                closeFlashlight()
                delay(5000)
            }
        }
        runBlocking {
            delay(Long.MAX_VALUE)
        }
    

    and also add this dependency in the build.gradle module level file

        implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9")
    

    And if you want to do it using handler you can try this I am not sure if this will work or not.

    val handler = Handler(Looper.getMainLooper())
    var isFlashlightOpen = false
    
    val toggleFlashlight = object : Runnable {
        override fun run() {
            if (isFlashlightOpen) {
                closeFlashlight()
                isFlashlightOpen = false
            } else {
                openFlashlight()
                isFlashlightOpen = true
            }
            handler.postDelayed(this, 5000)
        }
    }
    
    handler.postDelayed(toggleFlashlight, 5000)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search