android schedule 方法的作用,android下schedule方法

一值总觉得timer怪怪的,就是不知道怎么解释,这个解释正到好处。TimerTasks are not ideal to use in an android environment because

一值总觉得timer怪怪的,就是不知道怎么解释,这个解释正到好处。

TimerTasks are not ideal to use in an android environment because they're not context-aware. If your context goes away, the TimerTask will still wait patiently in the background, eventually firing and potentially crashing your app because its activity was previously finished. Or, it may keep references to your activity around after it's been closed, preventing it from being garbage collected and potentially making your app run out of memory.

Instead, use postDelayed(), which will automatically cancel the task when the activity is shut down.

finalintdelay=5000;finalintperiod=1000;finalRunnabler=newRunnable(){publicvoidrun(){Toast.makeText(getApplicationContext(),"RUN!",Toast.LENGTH_SHORT).show();postDelayed(this,period);}};postDelayed(r,delay);

By the way, if you ever need to cancel your task manually, you can use removeCallbacks(r) where r is the runnable you posted previously

发布者:admin,转转请注明出处:http://www.yc00.com/web/1755001450a5225557.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信