Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

235
Views
Cómo codificar un contador que se repite dentro de un intervalo en Kotlin Android

Estoy tratando de crear una vista de cuenta regresiva dentro de una aplicación de Android con Kotlin. El código que tengo actualmente bloquea la aplicación mientras se ejecuta.

PD: el timer enteros también debe modificarse más adelante en la aplicación.

 var timer: Int = 50 // Declared globally private fun timer(){ timer_text.text = timer.toString() // Text view in UI while (timer > 1){ Handler().postDelayed({ timer-- timer_text.text = timer.toString() // Update the Text view in UI }, 1000) } }

Logcat

 I/sandaru.projec: Thread[6,tid=11100,WaitingInMainSignalCatcherLoop,Thread*=0xb400007b3dcd9f50,peer=0x13280238,"Signal Catcher"]: reacting to signal 3 I/sandaru.projec: I/sandaru.projec: Wrote stack traces to tombstoned D/EGL_emulation: app_time_stats: avg=5.51ms min=1.98ms max=10.47ms count=60
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Parece que hay un ANR, ya que está ejecutando el ciclo while en el subproceso principal, puede intentar con el código a continuación, al hacer esto se suspenderá en lugar de bloquear

 var timer: Int = 50 // Declared globally private suspend fun timer(){ withContext(Dispatchers.Main) { timer_text.text = timer.toString() // Text view in UI while (timer > 1){ timer-- timer_text.text = timer.toString() // Update the Text view in UI delay(1000) } } }

y necesita ejecutar el código desde dentro de una rutina, por ej. en la actividad/fragmento puedes ejecutar así

 lifecycleScope.launch { timer() }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!