Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

How to code for SensorEventListener in Activity class by Xamarin

$
0
0

I'm get trouble for this topic.

First I have reference Java code.

private SensorEventListener mySensorEventListener = new SensorEventListener(){


public void onAccuracyChanged(Sensor sensor, int accuracy) {//onAccuracyChanged
  }
  @Override
  public void onSensorChanged(SensorEvent event) {
   if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){//Determine whether the change in the acceleration sensor
    float [] values = event.values;  //Captured data
    tvX.setText("acceleration on the x-axis direction is:"+values[0]);  
    tvY.setText("acceleration on the y-axis direction is"+values[1]); 
    tvZ.setText("acceleration on the z-axis direction is"+values[2]);  
   }  
  }
 };

It direct write under Activity class.

And I want copy this idea.

But C# and JAve is different. I don't know how to come ture this idea on Activity class.

I'm try this idea.

 public class MainActivity : Activity
    {

        private SensorManager sensormanager;
        private Sensor sensor_Accelerometer, sensor_MAGNETIC_FIELD;
        private SensorEvent Sensor_Evenlistener_1 ;




   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);  //Obtain services from a sensor system
        sensor_Accelerometer = sensormanager.GetDefaultSensor(SensorType.Accelerometer);
        sensor_MAGNETIC_FIELD = sensormanager.GetDefaultSensor(SensorType.MagneticField);
        sensormanager.RegisterListener(Sensor_Evenlistener_1, sensor_Accelerometer.Handle, SensorDelay.Normal);
        sensormanager.RegisterListener(Sensor_Evenlistener_1, sensor_MAGNETIC_FIELD.Handle, SensorDelay.Normal);                                      // Key

But about RegisterListener this part.

Always appear fail message Compiler.

Please help me. How to come true this idea by Xamarin.

Thanks!


Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>