创建一个Handler函数以随机生成5个坐标

时间:2018-12-10 10:23:34

标签: android

早上好,我需要有关此功能的帮助,我不了解处理程序的工作方式。 这是我当前的代码:

公共类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;
}

}

1 个答案:

答案 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);
}