我有以下活动代码:
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.os.Handler
import android.support.v7.app.AppCompatActivity
import com.mikhailovskii.androidacademytask8.R
import com.mikhailovskii.androidacademytask8.data.service.ProgressIntentService
import kotlinx.android.synthetic.main.activity_main.*
class ProgressActivity : AppCompatActivity() {
private val receiver = ActivityBroadcastReceiver(Handler())
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
tv_progress.text="0"
val intentFilter = IntentFilter(ProgressIntentService.INTENT_ACTION)
registerReceiver(receiver, intentFilter)
btn_intent_service.setOnClickListener {
val intent = Intent(this, ProgressIntentService::class.java)
startService(intent)
}
btn_service.setOnClickListener {
}
}
override fun onDestroy() {
super.onDestroy()
unregisterReceiver(receiver)
}
//FIXME handler1 seems strange, but passing to constructor doesn't work
class ActivityBroadcastReceiver (handler1: Handler) : BroadcastReceiver() {
private val handler = handler1
override fun onReceive(context: Context?, intent: Intent?) {
val progress = intent?.getIntExtra(ProgressIntentService.EXTRA_KEY_OUT, 0)
handler.post { tv_progress.text = progress }
}
}
}
我需要从tv_progress
更改BroadcastReceiver
中的文本。为此,我在这里添加了Handler。但是电视名称以红色突出显示,而Studio则写道
未解决的参考。由于接收器类型不匹配,以下候选者都不适用
那么,此问题的原因是什么,我该如何解决?预先感谢!
答案 0 :(得分:0)
请在下面的行中检查广播的名称:
val intentFilter = IntentFilter(ProgressIntentService.INTENT_ACTION)