英文:
Why is the flashlight blinking not starting when running the application from the phone?
问题
手机在通过Android Studio运行应用程序并将其构建并安装到手机上时,会启动,然后开始闪烁。但如果我从手机而不是从Android Studio启动应用程序,它就不会执行任何操作。
目标是在从Android Studio或手机本身启动应用程序时,使手机的闪光灯开始闪烁,但仅在从Android Studio启动时才有效。
package com.example.myflashlight
import android.content.Context
import android.hardware.camera2.CameraAccessException
import android.hardware.camera2.CameraCharacteristics.FLASH_INFO_AVAILABLE
import android.hardware.camera2.CameraManager
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import androidx.annotation.RequiresApi
class MainActivity : AppCompatActivity() {
private lateinit var cameraManager: CameraManager
private lateinit var cameraId: String
private lateinit var handler: Handler
private var isFlashOn = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
cameraManager = getSystemService(Context.CAMERA_SERVICE) as CameraManager
try {
cameraId = cameraManager.cameraIdList[0]
} catch (e: CameraAccessException) {
e.printStackTrace()
}
handler = Handler(Looper.getMainLooper())
handler.post(blinkRunnable)
}
private val blinkRunnable = object : Runnable {
@RequiresApi(Build.VERSION_CODES.M)
override fun run() {
if (isFlashOn) {
turnFlashOff()
} else {
turnFlashOn()
}
handler.postDelayed(this, 1000)
}
}
@RequiresApi(Build.VERSION_CODES.M)
private fun turnFlashOn() {
try {
cameraManager.setTorchMode(cameraId, true)
isFlashOn = true
} catch (e: CameraAccessException) {
e.printStackTrace()
}
}
@RequiresApi(Build.VERSION_CODES.M)
private fun turnFlashOff() {
try {
cameraManager.setTorchMode(cameraId, false)
isFlashOn = false
} catch (e: CameraAccessException) {
e.printStackTrace()
}
}
@RequiresApi(Build.VERSION_CODES.M)
override fun onPause() {
super.onPause()
handler.removeCallbacks(blinkRunnable)
if (isFlashOn) {
turnFlashOff()
}
}
}
英文:
It does start on the phone when I'm running the application through android studio and it's building and installing it to the phone then the blinking start. but if I start the application from the phone not from android studio then it does nothing.
The goal is to make the flashlight on the phone start blinking when starting the application either from the android studio or from the phone itself but it's working only from android studio.
package com.example.myflashlight
import android.content.Context
import android.hardware.camera2.CameraAccessException
import android.hardware.camera2.CameraCharacteristics.FLASH_INFO_AVAILABLE
import android.hardware.camera2.CameraManager
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import androidx.annotation.RequiresApi
class MainActivity : AppCompatActivity() {
private lateinit var cameraManager: CameraManager
private lateinit var cameraId: String
private lateinit var handler: Handler
private var isFlashOn = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
cameraManager = getSystemService(Context.CAMERA_SERVICE) as CameraManager
try {
cameraId = cameraManager.cameraIdList[0]
} catch (e: CameraAccessException) {
e.printStackTrace()
}
handler = Handler(Looper.getMainLooper())
handler.post(blinkRunnable)
}
private val blinkRunnable = object : Runnable {
@RequiresApi(Build.VERSION_CODES.M)
override fun run() {
if (isFlashOn) {
turnFlashOff()
} else {
turnFlashOn()
}
handler.postDelayed(this, 1000)
}
}
@RequiresApi(Build.VERSION_CODES.M)
private fun turnFlashOn() {
try {
cameraManager.setTorchMode(cameraId, true)
isFlashOn = true
} catch (e: CameraAccessException) {
e.printStackTrace()
}
}
@RequiresApi(Build.VERSION_CODES.M)
private fun turnFlashOff() {
try {
cameraManager.setTorchMode(cameraId, false)
isFlashOn = false
} catch (e: CameraAccessException) {
e.printStackTrace()
}
}
@RequiresApi(Build.VERSION_CODES.M)
override fun onPause() {
super.onPause()
handler.removeCallbacks(blinkRunnable)
if (isFlashOn) {
turnFlashOff()
}
}
}
答案1
得分: 0
如果您在onPause
中清除了处理程序,那么为了对称性,您应该在onResume
中启动第一个可运行项,而不是在onCreate
中。在Android SDK 30+上,当您重新打开应用程序时,onCreate()
可能不会被调用,因为它可能只是恢复同一个Activity实例。
在通过Android Studio启动时它可能能正常工作的原因可能是因为它可能强制完全重新启动整个应用程序,这将保证一个新的Activity实例和对onCreate()
的新调用。
英文:
If you're clearing the Handler in onPause
, then you should start the first runnable in onResume
(instead of onCreate
) for symmetry. onCreate()
might not be getting called when you reopen an app on Android SDK 30+ because it might just be resuming the same Activity instance.
The reason it might be working when launched through Android Studio is that it's probably forcing a complete restart of the whole application which would guarantee a new Activity instance and new call to onCreate()
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论