黑马程序员技术交流社区

标题: 写了一个可以记录路线的计步器,好像有内存泄露问题,求指点 [打印本页]

作者: joeywr    时间: 2015-8-25 23:12
标题: 写了一个可以记录路线的计步器,好像有内存泄露问题,求指点
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;
                }
        }       
程序刚开始运行一切正常,一段时间以后,会变得很卡,点击事件响应很慢,看了看内存占用偏高,是内存泄露问题吗,求指点。
作者: jumtop    时间: 2015-8-25 23:14
看不懂。
作者: 巧克黑力    时间: 2015-8-25 23:19
就这么简单。。。
作者: joeywr    时间: 2015-8-25 23:24
把handler.sendMessage(message);改成handler.obtainMessage(根据实际需要传参).sendToTarget(); .......




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2