2014-04-16 6 views

risposta

6

Ecco un esempio completo che utilizza Android.Hardware.ISensorEventListener per rilevare un gesto di scuotimento. Dovresti essere in grado di rilasciare questo nei tuoi progetti senza problemi.

[Activity (Label = "ShakeDetection", MainLauncher = true)] 
public class MainActivity : Activity, Android.Hardware.ISensorEventListener 
{ 
    bool hasUpdated = false; 
    DateTime lastUpdate; 
    float last_x = 0.0f; 
    float last_y = 0.0f; 
    float last_z = 0.0f; 

    const int ShakeDetectionTimeLapse = 250; 
    const double ShakeThreshold = 800; 

    protected override void OnCreate (Bundle bundle) 
    { 
     base.OnCreate (bundle); 

     SetContentView (Resource.Layout.Main); 

     // Register this as a listener with the underlying service. 
     var sensorManager = GetSystemService (SensorService) as Android.Hardware.SensorManager; 
     var sensor = sensorManager.GetDefaultSensor (Android.Hardware.SensorType.Accelerometer); 
     sensorManager.RegisterListener(this, sensor, Android.Hardware.SensorDelay.Game); 
    } 

    #region Android.Hardware.ISensorEventListener implementation 

    public void OnAccuracyChanged (Android.Hardware.Sensor sensor, Android.Hardware.SensorStatus accuracy) 
    { 
    } 

    public void OnSensorChanged (Android.Hardware.SensorEvent e) 
    { 
     if (e.Sensor.Type == Android.Hardware.SensorType.Accelerometer) 
     { 
      float x = e.Values[0]; 
      float y = e.Values[1]; 
      float z = e.Values[2]; 

      DateTime curTime = System.DateTime.Now; 
      if (hasUpdated == false) 
      { 
       hasUpdated = true; 
       lastUpdate = curTime; 
       last_x = x; 
       last_y = y; 
       last_z = z; 
      } 
      else 
      { 
       if ((curTime - lastUpdate).TotalMilliseconds > ShakeDetectionTimeLapse) { 
        float diffTime = (float)(curTime - lastUpdate).TotalMilliseconds; 
        lastUpdate = curTime; 
        float total = x + y + z - last_x - last_y - last_z; 
        float speed = Math.Abs(total)/diffTime * 10000; 

        if (speed > ShakeThreshold) { 
         Toast.MakeText(this, "shake detected w/ speed: " + speed, ToastLength.Short).Show(); 
        } 

        last_x = x; 
        last_y = y; 
        last_z = z; 
       } 
      } 
     } 
    } 
    #endregion 
} 

L'attività sopra implementa l'interfaccia Android.Hardware.ISensorEventListener e quindi registra attraverso il SensorManager. Gli eventi effettivi del sensore (scuotimento, ecc.) Sono convogliati su OnSensorChanged; è qui che teniamo la logica per il nostro codice di rilevamento delle vibrazioni.

Ho basato questa risposta su this one ma ho apportato alcune modifiche ad esso. In primo luogo, questa risposta utilizza ISensorEventListener anziché ISensorListener (che era obsoleto nel livello API 3). E troverai il rilevamento dei gesti iniziali incluso (tramite hasUpdated) e alcune variabili per controllare la sensibilità dell'oscillazione. Giocando con ShakeDetectionTimeLapse e ShakeDetectionThreshold dovresti essere in grado di adattarlo alle tue esigenze.

See:

  1. How to detect shake event with android?
  2. android SensorEventListener problem
+0

farò dare una prova e farvi sapere. grazie mille :) –

Problemi correlati