2014-04-16 11 views

Trả lời

6

Dưới đây là ví dụ hoàn chỉnh sử dụng Android.Hardware.ISensorEventListener để phát hiện cử chỉ lắc. Bạn sẽ có thể để thả này vào dự án của riêng bạn (s) mà không có bất kỳ rắc rối.

[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 
} 

Các hoạt động nêu trên thực hiện các giao diện Android.Hardware.ISensorEventListener và sau đó đăng ký nó thông qua SensorManager. Các sự kiện cảm biến thực tế (lắc vv) được dẫn vào OnSensorChanged; đây là nơi chúng tôi giữ logic cho mã phát hiện rung của chúng tôi.

Tôi đã dựa trên câu trả lời này trên this one nhưng đã thực hiện một vài sửa đổi đối với nó. Thứ nhất, câu trả lời này sử dụng ISensorEventListener thay vì ISensorListener (không được dùng ở cấp API 3). Và bạn sẽ tìm thấy phát hiện cử chỉ bắt đầu bao gồm (thông qua hasUpdated) và một số biến để kiểm soát độ nhạy của lắc. Bằng cách chơi xung quanh với ShakeDetectionTimeLapseShakeDetectionThreshold bạn sẽ có thể tinh chỉnh nó theo nhu cầu của mình.

Xem:

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

tôi sẽ cung cấp cho một thử này & cho bạn biết. cảm ơn bạn rất nhiều :) –

Các vấn đề liên quan