1

I want to get the android's accelerometer (x y z coordinates) like this code:

http://www.techrepublic.com/blog/software-engineer/a-quick-tutorial-on-coding-androids-accelerometer/ (function: onSensorChanged(SensorEvent event))

But I can't to catch events from my code because I'm not in the activity class.

Does anyone have an idea how to access this information without access to events?

Rotem E
  • 213
  • 2
  • 10

1 Answers1

0

in MainActivity create a global variable create an object with type of other class like "accelerometer"

public static SensorManager sensor_manager = null;
otherclass accelerometer = new otherclass();

in oncreate of the MainActivity get sensor service

sensor_manager = (SensorManager) getSystemService(SENSOR_SERVICE);
accelerometer.function();

in your other class create a function

function()
{
SensorManager sensor_manager = MainActivity.sensor_manager;
sensor_manager.registerListener(this,
            sensor_manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
            SensorManager.SENSOR_DELAY_NORMAL);
}
fatih inan
  • 144
  • 1
  • 6