0

我正在使用教程为 android 编写加速度计。但是我遇到了一些我不确定的讨厌的问题。这是代码:

protected override void OnResume()

    {

        base.OnResume ();

        _sensorManager.RegisterListener (this, _sensorManager.GetDefaultSensor (SensorType.Accelerometer), SensorDelay.Ui); // this is where I get " cannot convert from "MotionDector.1 to Android.Hardware.ISensorEventListener" 

    }



    protected override void OnPause()

    {

        base.OnPause ();

        _sensorManager.UnregisterListener (this); // Same problem here

    }

我已经包含了 android.hardware 命名空间。我的项目名称是 MotionDector

4

1 回答 1

1

在这里工作正常。你还记得ISensorEventListener你的课堂宣誓吗?我使用与您链接到的完全相同的方法。

using System.Text;
using Android.App;
using Android.Hardware;
using Android.Widget;
using Android.OS;

namespace AccelerometerShiz
{
    [Activity(Label = "AccelerometerShiz", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : Activity, ISensorEventListener
    {
        private SensorManager _sensorManager; 
        private TextView _sensorTextView; 
        private static readonly object SyncLock = new object();

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

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            _sensorManager = (SensorManager)GetSystemService(SensorService);
            _sensorTextView = FindViewById<TextView>(Resource.Id.accelerometer_text);
        }

        protected override void OnResume()
        {
            base.OnResume();
            _sensorManager.RegisterListener(this, _sensorManager.GetDefaultSensor(SensorType.Accelerometer), SensorDelay.Ui);
        }

        protected override void OnPause()
        {
            base.OnPause();
            _sensorManager.UnregisterListener(this);
        }

        public void OnAccuracyChanged(Sensor sensor, SensorStatus accuracy)
        {
            // We don't want to do anything here.
        }

        public void OnSensorChanged(SensorEvent e)
        {
            lock (SyncLock)
            {
                var text = new StringBuilder("x = ")
                    .Append(e.Values[0])
                    .Append(", y=")
                    .Append(e.Values[1])
                    .Append(", z=")
                    .Append(e.Values[2]);
                _sensorTextView.Text = text.ToString();
            }
        }
    }
}
于 2013-07-02T23:07:13.317 回答