1

我正在使用处理程序及其 postdelayed 方法在每 10 秒后执行一次可运行。但看起来它每秒执行一次。这是代码:

    final Handler handler = new Handler();
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            try {
                if (oneTimeExecution
                        && !GcmIntentService.agreedId.equals(null)) {
                    oneTimeExecution = false;

                    Iterator it = JsonAvailabeInfos.InfoIdentification
                            .entrySet().iterator();
                    while (it.hasNext()) {
                        Map.Entry pairs = (Map.Entry) it.next();
                        InfoInfo tempInfo = (InfoInfo) pairs
                                .getValue();
                        if (!tempInfo.getId().equals(
                                GcmIntentService.agreedInfoId)) {
                            Marker m = (Marker) pairs.getKey();
                            m.setVisible(false);
                        } else {
                             myInfoMarker = (Marker) pairs.getKey();
                        }

                    }

                }
                if (!GcmIntentService.agreedInfoId.equals(null)) {
                    GetSelectedInfoLocation InfoUpdatedLocation = new GetSelectedInfoLocation();
                    JSONObject jobj = InfoUpdatedLocation.execute("123").get();
                    //      GcmIntentService.agreedInfoId).get();
                    double lat=Double.parseDouble(jobj.getString("lat"));
                    double lon=Double.parseDouble(jobj.getString("lon"));
                    Log.i("hello", "inside handler that is executed in every 10 seconds");
                    LatLng loc= new LatLng(lat, lon);
                    myInfoMarker.setPosition(loc);

                }
                handler.postDelayed(this, 10000);
            } catch (Exception e) {
                Log.e("InfoLocationUpdate",e.toString());
            } 
        }
    };

boolean tempFlag=true;
    if(tempFlag){
    handler.postDelayed(runnable, 10000);
    tempFlag=false;
    }

仅供参考,最后几行是为了确保 runnable 被调用一次,然后这个 runnable 被递归调用。谁能告诉我为什么应该在 10 秒后调用它时每秒调用一次?

4

0 回答 0