我对Android开发非常陌生,我也没有真正了解IntentServices的工作方式。这是我到目前为止有:在后台运行服务
class TimerActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_timer)
findViewById(R.id.startBtn).setOnClickListener {
startService(Intent([email protected], TimerService::class.java))
}
findViewById(R.id.stopBtn).setOnClickListener {
stopService(Intent([email protected], TimerService::class.java))
}
}
}
class TimerService : IntentService("TimerService") {
override fun onCreate(){
super.onCreate()
toast("Started activity")
}
override fun onDestroy(){
super.onDestroy()
toast("Stopped activity")
}
override fun onHandleIntent(intent: Intent?) {
}
}
我试图启动一个按钮的点击背景的活动,并停止它的另一个的点击。但是为什么onDestroy()会在onCreate()之后立即被触发,而我没有点击停止按钮?
谢谢
也许是因为你有空的'onHandleIntent' –
当onHandleIntent()完成其工作时,Intent服务会自行停止。 –