1

I am using handler and its postdelayed method to execute a runnable after every 10 seconds. but It looks like it is executed every second. Here is the code:

    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 last few lines are to ensure that the runnable is called once and than again this runnable is called recursively. Can anybody tell me why is it getting called every second when it should be called after 10 seconds?

Puneetr90
  • 199
  • 1
  • 6
  • 18

0 Answers0