无法将motiondector.activity1转换为android.hardware

本文关键字:android hardware 转换 activity1 motiondector | 更新日期: 2023-09-27 18:20:43

我正在使用本教程为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

无法将motiondector.activity1转换为android.hardware

在这里运行良好。你还记得你下课时的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();
            }
        }
    }
}