是否可以在活动环境之外使用加速度计?

时间:2013-05-07 22:45:45

标签: android eclipse android-activity accelerometer

嗨,我是新的(我只是编程了几个月,所以我的掌握是不稳定的)日食,并试图创建一个游戏,其中玩家的化身移动加速度计,他们必须躲避屏幕上的东西(从上到下的角度来看)。我已经创建了一个gameScreen类来绘制头像,背景和敌人,我正在尝试为加速度计创建一个类来告诉gameScreen在哪里移动头像。目前我的类包含加速度计扩展AndroidGame框架的代码,但由于它不是活动而且没有onCreate,我不知道如何设置加速度计。有没有办法做到这一点,或者我只是这样做错了吗?

这是我到目前为止所做的事情(显然不完整)。

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
}
}

1 个答案:

答案 0 :(得分:2)

在您的accelerometer类中,使用Context参数创建一个构造函数,并使用此参数来实例化SensorManager。

public MyAccelerometerClass implements SensorEventListener
{
     private SensorManager sensorManager;

     public MyAccelerometerClass(Context context)
     {
          sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); 
     }

     // codes 
 }