2011-12-02 70 views
4

I'm與加速計和第一代碼工作運行兩個FOR循環是震動探測器: (代碼1)如何在同一時間

if (sensor == SensorManager.SENSOR_ACCELEROMETER) { 
     long curTime = System.currentTimeMillis(); 
     long now = System.currentTimeMillis(); 

     //********************************************************************** 
     if ((now - mLastForce) > SHAKE_TIMEOUT) { 
       mShakeCount = 0; 
      } 

      if ((now - mLastTime) > TIME_THRESHOLD) { 
       long diff = now - mLastTime; 
       float speed = Math.abs(x + y + z - mLastX - mLastY - mLastZ)/diff * 10000; 
       if (speed > FORCE_THRESHOLD) { 
       if ((++mShakeCount >= SHAKE_COUNT) && (now - mLastShake > SHAKE_DURATION)) { 
        mLastShake = now; 
        mShakeCount = 0; 
        if (mShakeListener != null) { 
        mShakeListener.onShake(); 
        } 
       } 
       mLastForce = now; 
       } 
       mLastTime = now; 
       mLastX = x; 
       mLastY = y; 


       mLastZ = z; 

有了這個,我得到消息,當壽手機振盪: (代碼2)

mSensorListener.setOnShakeListener(new OnShakeListener() { 

     @Override 
     public void onShake() { 
      // TODO Auto-generated method stub 
      Toast.makeText(getApplicationContext(), "Shake!", Toast.LENGTH_SHORT).show(); 
     } 
    }); 

我還用於循環保存加速度值x,y和z爲陣列,每2個secons: (CODE3)

if (lastUpdate == -1 || (curTime - lastUpdate) > 2000) { 
      lastUpdate = curTime; 

      x = values[0]; 
      y = values[1]; 
      z = values[2]; 

       for (int column = 0; column < 3; column++) { 
        if (column == 0) { 
         p[row][column] = values[0]; 

        } 
        if (column == 1) { 
         p[row][column] = values[1]; 
         //yacc.setText("Os X: " + p[row][column]); 
        } 
        if (column == 2) { 
         p[row][column] = values[2]; 
         //zacc.setText("Os X: " + p[row][column]); 
        }} 
        if (row == 0) { 
         xacc.setText("Os X: " + p[row][0] + " " + p[row][1] +" " + p[row][2]); 
        } 
        if (row == 1) { 
         yacc.setText("Os X: " + p[row][0] + " " + p[row][1] +" " + p[row][2]); 
        } 
        if (row == 2) { 
         zacc.setText("Os X: " + p[row][0] + " " + p[row][1] +" " + p[row][2]); 
        } 
        if (row == 3) { 
         z2acc.setText("Os X: " + p[row][0] + " " + p[row][1] +" " + p[row][2]); 
        } 
        if (row == 4) { 
         z3acc.setText("Os X: " + p[row][0] + " " + p[row][1] +" " + p[row][2]); 
        } 
        row++; 
       if (row == 5) { 
        row = 0; 
       } 

Code3永不結束,code1是抖動檢測器。我怎麼能一起運行,也許與線程(如何)或其他任何東西?

+7

如果您想要同時運行兩個循環(或更多),請使用線程。剛剛定義每個循環在一個線程中,然後啓動線程:) – Houcine

回答

8

,如果你想運行在同一時間兩個迴路(或以上),使用Threads。只是定義了每個循環在一個線程,然後啓動你的線程:)

例子:

第一線:

public class ThreadForLoopA extends Thread{ 
    // variables for your Thread ... 
    @Override 
    public void run(){ 
     // your first loop here ... 
    } 
} 

第二個線程:

public class ThreadForLoopB extends Thread{ 
    // variables for your Thread ... 
    @Override 
    public void run(){ 
     // your second loop here ... 
    } 
} 

啓動所有的這樣的線程:

ThreadForLoopA threadA = new ThreadForLoopA(); 
ThreadForLoopB threadB = new ThreadForLoopB(); 

//start threads (the two loops will be executed at the same time) 
threadA.start(); 
threadB.start(); 
+0

@Override \t公共無效onSensorChanged(INT傳感器,浮動[]值){ \t \t // TODO自動生成方法存根 \t \t \t \t x =值[0]; \t \t y = values [1]; \t \t z = values [2]; – JanOlMajti

+0

如何從onSensorChanged獲取加速計值到ThreatForLoopA? – JanOlMajti

+0

將它們作爲參數傳遞給構造函數。 – Houcine