Timer 和 Handler的相关代码如下:
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg){
super.handleMessage(msg);
if(msg.what == 1){
countDistance();
countStep();
circleBar.setProgress(total_step, target_distance);
if(flag){
circleBar.startCustomAnimation();
flag = false;
}
if (timer != 0 && distance != 0.0){
calories = weight * total_step* step_length * 1.036 * 0.001;
calory_consume = calories;
} else {
calories = 0.0;
}
if(total_step == target_distance){
Intent service = new Intent(getActivity(),StepCounterService.class);
getActivity().stopService(service);
}
circleBar.setMax(target_distance);
//更新ui
tv_distance.setText(formatDouble(distance));
tv_calory.setText(formatDouble(calories));
tv_sugar.setText(formatDouble(calories / 4.1));
tv_time.setText(getFormatTime(timer));
//定位相关
if(isStartLoacation){
//防止信号不好的时候p1和p2为空,异常
if(p1 != null && p2 != null){
pointstwo.add(p1);
pointstwo.add(p2);
OverlayOptions ooPolyline = new PolylineOptions().width(4).color(0xAAFF0000).points(pointstwo);
mOverlay2 = mBaiduMap.addOverlay(ooPolyline);
p1 = p2;
mLocClient.requestLocation();
}else{
mLocClient.requestLocation();
}
}
}
}
};
public void startTimer() {
if (mtimer == null) {
mtimer = new Timer();
}
if (mtask == null) {
mtask = new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
if (StepCounterService.flag) {
if(startTimer != System.currentTimeMillis()){
timer = tempTimer + System.currentTimeMillis() - startTimer;
}
}
}
};
}
if (mtimer != null && mtask != null) {
mtimer.schedule(mtask, 0, 200);
}
}
public void pauseTimer() {
if (mtimer != null) {
mtimer.cancel();
mtimer = null;
}
if (mtask != null) {
mtask.cancel();
mtask = null;
}
}
程序刚开始运行一切正常,一段时间以后,会变得很卡,点击事件响应很慢,看了看内存占用偏高,是内存泄露问题吗,求指点。 |
|