早上好,我需要有关此功能的帮助,我不了解处理程序的工作方式。 这是我当前的代码:
公共类GeneraMarker {
static LatLng GenPosition(Double Lat, Double Long) {
final Handler gen = new Handler();
Runnable gener = new Runnable() {
@Override
public void run() {
if (counter) {
//Random coordinates generator
} else {
gen.removeCallbacks(null);
gen.removeCallbacksAndMessages(null);
}
gen.postDelayed(this, 600000);
}
};
gen.post(gener);
return aPosition;
}
}
答案 0 :(得分:1)
Handler
收到一个Runnable
对象,以运行代码块。并且它的执行与方法执行是分开的。因此您必须创建一个监听器来监听生成的latlang
,如下所示
static void GenPosition(Double Lat, Double Long,ListenerClass listener) {
final Handler gen = new Handler();
Runnable gener = new Runnable() {
@Override
public void run() {
if (counter) {
//Random coordinates generator
listener.generatedLatLang(newLatLang);
} else {
gen.removeCallbacks(null);
gen.removeCallbacksAndMessages(null);
}
gen.postDelayed(this, 600000);
}
};
gen.post(gener);
}