2015-01-04 88 views
1

我一直在使用這種代碼Android: I want to shake it來實現震動運動檢測器。但實際上,當我在智能手機上運行它時,它什麼都不做。我嘗試使用斷點檢查,並且它從不輸入if(mAccel> 12)。 我試圖動搖它真的很難,但仍然不承認。我認爲,這需要一定的權限,我加入這一行的體現:Acelerometer:搖晃手機檢測

<uses-feature android:name="android.hardware.sensor.accelerometer" android:required="true" /> 

我試着在銀河S3和萬普拉斯之一,我忘了什麼東西?

EspecificExerciseActivity.java

public class EspecificoExercicio extends Activity { 

    private ArrayList<Exercicio> listaExercicios; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_especifico_exercicio); 

     /* do this in onCreate */ 
     mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 
     mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL); 
     mAccel = 0.00f; 
     mAccelCurrent = SensorManager.GRAVITY_EARTH; 
     mAccelLast = SensorManager.GRAVITY_EARTH; 

     if (mAccel > 12) { 
      Toast toast1 = Toast.makeText(getApplicationContext(), "Device has shaken.", Toast.LENGTH_LONG); 
      toast1.show(); 
     } 
    } 

     /* put this into your activity class */ 
     private SensorManager mSensorManager; 
     private float mAccel; // acceleration apart from gravity 
     private float mAccelCurrent; // current acceleration including gravity 
     private float mAccelLast; // last acceleration including gravity 

     private final SensorEventListener mSensorListener = new SensorEventListener() { 

     public void onSensorChanged(SensorEvent se) { 
      float x = se.values[0]; 
      float y = se.values[1]; 
      float z = se.values[2]; 
      mAccelLast = mAccelCurrent; 
      mAccelCurrent = (float) Math.sqrt((double) (x*x + y*y + z*z)); 
      float delta = mAccelCurrent - mAccelLast; 
      mAccel = mAccel * 0.9f + delta; // perform low-cut filter 
     } 

     public void onAccuracyChanged(Sensor sensor, int accuracy) { 
      if (mAccel > 4) { 
       Toast toast1 = Toast.makeText(getApplicationContext(), "DevSSDAASSADA", Toast.LENGTH_LONG); 
       toast1.show(); 
      } 
     } 


     }; 

     @Override 
     protected void onResume() { 
     super.onResume(); 
     mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL); 

     } 

     @Override 
     protected void onPause() { 
     mSensorManager.unregisterListener(mSensorListener); 
     super.onPause(); 
     } 

} 

回答

1

你檢查你的if (mAccel > 12)裏面的onCreate()。將支票移至onSensorChanged()

+0

還是一樣@Sleelight – 2015-01-04 14:02:34

+0

如果你把onSensorChanged()斷點,你到達那裏? – Steelight 2015-01-04 14:05:14

+0

yeas @Steelight,但它沒有輸入if。我應該改變價值,而不是12,減少? – 2015-01-04 14:12:49