Home > Back-end >  AndroidTV IPTV change channel by number input
AndroidTV IPTV change channel by number input

Time:12-05

Need to change channel by numpad of remote controller but don't know correct method. Numbers can be pressed multiple times and need to wait for all of them and get 1 String like "123".

Now I override onKey up like below

override fun onKeyUp(keyCode: Int, event: KeyEvent): Boolean {
    return when(keyCode){
        KeyEvent.KEYCODE_1->{
            //I don't know how to wait here next key up and get correct full channel number
            true

        }
        KeyEvent.KEYCODE_2->{
            ...
            true
        }
        //EPG
        KeyEvent.KEYCODE_3->{
            ...
            true
        }
        ...
        ...
        
        else -> super.onKeyUp(keyCode, event)
    }
}

CodePudding user response:

  • To perform searching you need first to collect all digits of the number in range of some time, you can use StringBuilder to append one digit inside onKeyUp or OnKeyDown depend on your requirements

  • You need to delay performing search until the user write the full number, you can use CountDownTimer and reset the time every time the use write new digit (You can also create a progress bar represent the timer and update it) or you can use simple Timer

  • When the time is finish you should perform the search operation and clear the last number in StringBuilder and Update UI

val channelNumber = StringBuilder()
val numberSearchTimer : CountDownTimer?

override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
    return when(keyCode){
        KeyEvent.KEYCODE_1 -> {
            cannelNumber.append("1")
            perfomNumberSearch()
            true
        }
        KeyEvent.KEYCODE_2 -> {
            cannelNumber.append("2")
            perfomNumberSearch()
            true
        }
        ...
        
        else -> super.onKeyUp(keyCode, event)
    }
}

private fun perfomNumberSearch() {
    // Update UI With the new number
    binding.searchChannelNumber.text = channelNumber.toString()
    
    // Cancel the current time of it exists
    if (numberSearchTimer != null) numberSearchTimer.cancel()
    
    numberSearchTimer = object : CountDownTimer(1000, 100) {
        override fun onTick(millisUntilFinished: Long) {
             // Update UI With a progress until it perform search
             // or replace CountDownTimer with Timer
        }
        
        override fun onFinish() {
            changeChannelByNumber(channelNumber.toString())
            
            
            // Clear the last search number after performing it
            channelNumber.clear()
            binding.searchChannelNumber.text = channelNumber.toString()
        }
    }.start()
}
  • Related