Search code examples
androideclipseandroid-activityaccelerometer

Is it possible to use the accelerometer outside of the context of an activity?


Hi I am new (I have only been programming for a few months, so my grasp is shaky) to eclipse and am trying to create a game where the player's avatar is moved with the accelerometer and they must dodge things on the screen (from a top down perspective). I have already created a gameScreen class to draw the avatar, background, and enemies, and I am trying to create a class for the accelerometer to tell the gameScreen where to move the avatar. Currently I have the class that contains the code for the accelerometer extending a AndroidGame framework, but as it is not an activity and doesn't have an onCreate, I'm not sure how to set up the accelerometer. Is there a way to do this or am I just going about this all wrong?

This is what I have so far (it's obviously incomplete).

import edu.austincc.testlearnand.SplashLoadingScreen;
import edu.austincc.framework.Screen;
import edu.austincc.testlearnand.Assets;
import edu.austincc.framework.implementation.AndroidGame;
import android.content.Context;
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 BuffaloStampede extends AndroidGame implements SensorEventListener {
TextView textView;
SensorManager manager;
double[] gravity = new double[3];
public double[] linear_acceleration = new double[3];

boolean firstTimeCreate = true;

@Override
public Screen getInitScreen() {

    if (firstTimeCreate) {
        Assets.loadBS(this);
        firstTimeCreate = false;
    }

    return new SplashLoadingScreen(this);

}

@Override
public void onBackPressed() {
    getCurrentScreen().backButton();
}


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

    Assets.theme.play();

}

@Override
public void onPause() {
    super.onPause();
    Assets.theme.pause();
    manager.unregisterListener(this);

}







public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    textView = new TextView(this);

    manager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
    if (manager.getSensorList(Sensor.TYPE_ACCELEROMETER).size() == 0) {
        textView.setText("No accelerometer installed");
    } else {
        Sensor accelerometer = manager.getSensorList(
                Sensor.TYPE_ACCELEROMETER).get(0);
        if (!manager.registerListener(this, accelerometer,
                SensorManager.SENSOR_DELAY_NORMAL)) {
            textView.setText("Couldn't register accelerometer listener");
        }
    }
    setContentView(textView);
}

@Override
public void onSensorChanged(SensorEvent event) {
    // alpha is calculated as t / (t + dT)
    // with t, the low-pass filter's time-constant
    // and dT, the event delivery rate

    final double alpha = 0.8;

    gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
    gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
    gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];

    linear_acceleration[0] = event.values[0] - gravity[0];
    linear_acceleration[1] = event.values[1] - gravity[1];
    linear_acceleration[2] = event.values[2] - gravity[2];

}

public double[] getGravity() {
    return gravity;
}

public double[] getLinear_acceleration() {
    return linear_acceleration;
}

public void setGravity(double[] gravity) {
    this.gravity = gravity;
}

public void setLinear_acceleration(double[] linear_acceleration) {
    this.linear_acceleration = linear_acceleration;
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // nothing to do here
}
}

Solution

  • In your accelerometer class create a constructor with a Context parameter and use this parameter to instantiate a SensorManager.

    public MyAccelerometerClass implements SensorEventListener
    {
         private SensorManager sensorManager;
    
         public MyAccelerometerClass(Context context)
         {
              sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); 
         }
    
         // codes 
     }