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

时间:2016-11-25 14:30:22

标签: java android gps locationlistener

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

  1. 等待用户在起点航点的某个距离内
  2. 一旦开始航点,开始记录gps数据和当前时间的计时器
  3. 越过末端航路点时停止计时器
  4. 目前,我的开始和结束路标都是硬编码的,但我似乎遇到了一个错误,我一直试图跟踪我的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;
        }
    

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

    任何建议都会有所帮助,请提前感谢您的建议!

1 个答案:

答案 0 :(得分:1)

你的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();