繁体   English   中英

使用android studio获取两点之间的旅行时间的问题

[英]Issues using android studio to get time of travel between two points

在上周,我一直在使用Android Studio编写实现以下目标的代码:

  1. 等待用户在起始航点的一定距离内
  2. 到达起点后,开始一个计时器,记录gps数据和当前时间
  3. 越过终点时停止计时器

目前,我已经对起点和终点航路点进行了硬编码,但是似乎遇到了一个错误,我一直试图在我的IDE上通过逐步执行功能进行跟踪,但似乎找不到它。 以下是我一直在使用的代码:

void StartTimer (View view){
        //Location l = null;
        boolean hasLoc = false; //are we at the start?
        float speed = 0;
        float topSpeed = 0;


        while(hasLoc == false && cancel == false){
            float d = l.distanceTo(t);

            if(d < 2.0)
                hasLoc = true;

            //if(!l.equals(lm.getLastKnownLocation("")))
            String msg = "Latitude: " + l.getLatitude() + "\nLongitude: "+ l.getLongitude();
            Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG).show();
        }

        hasLoc = false;

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                // Actions to do after 10 seconds
                buzzer();
            }
        }, 10000);

        while(l.distanceTo(tf) > 2.0 && cancel == false){
            float cSpeed = l.getSpeed();

            if(cSpeed>topSpeed)
                topSpeed = cSpeed;

            String msg = "Current Speed: "+cSpeed+"Top Speed: "+topSpeed;
            Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG).show();
        }

        cancel = false;
    }

当我运行代码时,我测试过的手机会运行它,但是它不会响应,这使我相信存在一个我没有考虑过的不满意的循环。

任何建议都会有所帮助,在此先感谢您的建议!

您的while循环阻塞了CPU的执行,这就是导致它无法响应的原因。 相反,您应该将代码放在线程内,然后调用Thread.sleep(1000); 在线程内部,这样,每次执行内部代码后,while循环都会暂停1秒。

像这样:

  new Thread(new Runnable() {
        @Override
        public void run() {

            while (hasLoc == false && cancel == false) {
                float d = l.distanceTo(t);
                if (d < 2.0)
                    hasLoc = true;
                String msg = "Latitude: " + l.getLatitude() + "\nLongitude: " + l.getLongitude();

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG).show();
                    }
                });
            }

            hasLoc = false;

            new Handler().postDelayed(new Runnable() {
                public void run() {
                    // Actions to do after 10 seconds
                    buzzer();
                }
            }, 10000);

            while (l.distanceTo(tf) > 2.0 && cancel == false) {
                float cSpeed = l.getSpeed();

                if (cSpeed > topSpeed)
                    topSpeed = cSpeed;

                String msg = "Current Speed: " + cSpeed + "Top Speed: " + topSpeed;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG).show();
                    }
                });
            }

            cancel = false;

        }
    }).start();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM