2015-11-14 121 views
2

我正在處理的任務是需要檢測到手機墜落。以下是我正在使用的代碼,但它不符合標準。它僅在10次中檢測到1次。有什麼辦法可以優化下面的代碼嗎?Android:使用加速度計檢測手機墜落

@Override 
    public void onSensorChanged(SensorEvent sensorEvent) { 
     // TODO Auto-generated method stub 
     Sensor mySensor = sensorEvent.sensor; 

     if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) { 
      index++; 
      accelValuesX[index] = sensorEvent.values[0]; 
      accelValuesY[index] = sensorEvent.values[1]; 
      accelValuesZ[index] = sensorEvent.values[2]; 
      if(index >= 127){ 
       index = 0; 
       accelManage.unregisterListener(this); 
       callFallRecognition(); 
       accelManage.registerListener(this, senseAccel, SensorManager.SENSOR_DELAY_NORMAL); 
      } 
     } 
    } 


    public void callFallRecognition(){ 
     float prev = 0; 
     float curr = 0; 
     prev = 10; 
     for(int i=11;i<128;i++){ 
      curr = accelValuesZ[i]; 
      if(Math.abs(prev - curr) > 20){ 
       Toast.makeText(this, "Fall detected", Toast.LENGTH_LONG).show(); 
       sendSMS(); 
      } 

     } 


    } 

回答

2

發現這個方法更加準確..

  accelValuesX[index] = sensorEvent.values[0]; 
      accelValuesY[index] = sensorEvent.values[1]; 
      accelValuesZ[index] = sensorEvent.values[2]; 

      rootSquare=Math.sqrt(Math.pow(accelValuesX[index],2)+Math.pow(accelValuesY[index],2)+Math.pow(accelValuesZ[index],2)); 
      if(rootSquare<2.0) 
      { 

      Toast.makeText(this, "Fall detected", Toast.LENGTH_SHORT).show(); 

      } 

編號:http://www.jcomputers.us/vol9/jcp0907-07.pdf

+0

你能請張貼滿級?這將非常有幫助,我無法正確地理解這一點。 –

+0

https://github.com/Rakesh627/PhoneFinder--Android 這裏你去 – TheLion