Android 处理程序执行速度比预期快
我使用handler及其postdelayed方法每10秒执行一次runnable。但它看起来像是每秒钟执行一次。代码如下:Android 处理程序执行速度比预期快,android,handler,runnable,postdelayed,Android,Handler,Runnable,Postdelayed,我使用handler及其postdelayed方法每10秒执行一次runnable。但它看起来像是每秒钟执行一次。代码如下: final Handler handler = new Handler(); Runnable runnable = new Runnable() { @Override public void run() { try { if (oneTimeExecution
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秒后被呼叫