Call Class Magnetic Sensor in activity main Android Studio

1

Good evening experts

I have the following code from a class of type Main that uses the sensor Magnético of Android and shows the values in textView :

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class SensorMagnetic extends Activity  implements SensorEventListener {

private TextView magneticX;
private TextView magneticY;
private TextView magneticZ;
private SensorManager sensorManager = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    magneticX = (TextView) findViewById(R.id.valMag_X);
    magneticY = (TextView) findViewById(R.id.valMag_Y);
    magneticZ = (TextView) findViewById(R.id.valMag_Z);

    sensorManager.registerListener(this,
            sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
            SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
protected void onPause() {

    sensorManager.unregisterListener(this);
    super.onPause();
}

@Override
protected void onStop() {
    sensorManager.unregisterListener(this);
    super.onStop();
}

@Override
protected void onResume() {
    super.onResume();

    sensorManager.registerListener(this,
            sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
            SensorManager.SENSOR_DELAY_NORMAL);
}

public void onAccuracyChanged(Sensor sensor, int accuracy) {

}

public void onSensorChanged(SensorEvent sensorEvent) {
    synchronized (this) {
        if (sensorEvent.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
            magneticX.setText( Float.toString( sensorEvent.values[0]));
            magneticY.setText( Float.toString( sensorEvent.values[1]));
            magneticZ.setText( Float.toString( sensorEvent.values[2]));
        }
    }

   }
}

but I need to "call it" so to speak from a Main type Activity that has implemented methods and that therefore I can not put another, example:

public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {
/*. etc etc... (esta clase tiene un método onCreate y es de tipo Launcher)

How can I use my class SensorMagnetic in my Class MainActivity thanks in advance I hope you can help me

    
asked by user2091725 21.09.2016 в 23:54
source

1 answer

1

I imagine that what you want is to use your class SensorMagnetic in MainActivity , remember that SensorMagnetic is actually a Activity and there is no way to use it in MainActivity .

What you must do is that your MainActivity class must implement SensorEventListener :

    public class MainActivity extends Activity  implements SensorEventListener {
...
...

When you do this you must implement the methods:

@Override
public void onSensorChanged(SensorEvent sensorEvent) {

}

@Override
public void onAccuracyChanged(Sensor sensor, int i) {

}

and add the SensorManager references that are in your class SensorMagnetic , in short you must add the code you perform in your class SensorMagnetic .

    
answered by 22.09.2016 в 01:51