我子类化了TimerTask
使用匿名“具体”类,这样:
public void setTimedTask() {
/* Note: 'this' implements an interface called UpdateIndicatorsReceiver */
final UpdateIndicatorsReceiver receiver = this;
final TimerTask timerTask = new TimerTask() {
@Override
public void run() {
/* The interface UpdateIndicatorsReceiver has an updateIndicators method */
receiver.updateIndicators();
}
};
/* Code to actually set the timer.... */
}
请注意声明本地的奇怪之处final
字段命名receiver
并将其设置为this
。有没有更干净的方法来获得不改变的引用this
,以便在run
匿名类的方法?
这应该是不必要的;你应该简单地调用updateIndicators()
直接地。
i.e.:
public void run() {
updateIndicators();
}
无需receiver
参考。这为我编译。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)