处理程序执行速度超过预期

时间:2014-03-06 12:45:57

标签: android handler runnable postdelayed

我使用handler及其postdelayed方法在每10秒后执行一次runnable。但看起来它每秒都会执行一次。这是代码:

    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;
    }

FYI最后几行是为了确保runnable被调用一次,而且再次调用此runnable。任何人都可以告诉我,为什么它会在10秒后调用时每秒调用一次?

0 个答案:

没有答案