UpdateHandler, do a thing only when trigger is true, Android
I have a handler that receives a message on every location update, every 5 secs. From the location I can request the current city the user is moving in. Then I want to download some data once and only when city changes. How can I realize that?
Handler myViewUpdateHandler = new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case UPDATE_LOCATION:
mc.animateTo(geosenseo.getCurrentPoint());
mapView.invalidate();
//a async task sets the currentCity field
i开发者_如何学Cf(trigger && (currentCity != "")){
firstCity = currentCity;
trigger = false;
}
if((currentCity != "") && (firstCity != "")){
if(firstCity != currentCity){
//download only when city changes
trigger = true;
}
}
}
super.handleMessage(msg);
}
};
like you can see I have played around with the vars. any ideas? thanks
This helped...compare strings with equals and the following if statement
if(!currentCity.equals("")){
if(!firstCity.equals(currentCity)){
firstCity = currentCity;
}
}
精彩评论