我正在尝试开发一个应用程序,将加速度计和陀螺仪数据存储到sqlite数据库中。我已经做了让加速度计和陀螺仪工作的基本工作。我还成功地创建了一个工作数据库,当应用程序打开时,它会连续记录数据,并包含时间戳。我现在的问题是,我想包括一个开关开始/停止记录数据到数据库,以节省电池和内存,但我不能让它正常工作。我尝试过使用oncheckedchanged()来查看是否按下了切换,但这只会将一行数据添加到数据库中。我想要的是当切换被按下时,数据库中应该充满传感器数据,直到再次按下切换。有什么办法吗?任何反馈都是有用的。谢谢
// for accessing accelerometer and gyroscope sensors and displaying them
public class MainActivity extends AppCompatActivity implements LocationListener , SensorEventListener {
DatabaseHelper myDb;
private static final String TAG = "MainActivity";
private TextView xText, yText , zText, xTextGyro, yTextGyro , zTextGyro;
private ToggleButton toggle;
private Sensor accelerometer;
private Sensor gyroscope;
private SensorManager sM;
private SensorEventListener accelerometerListener,gyroscopeEventListener;
public float accelX, accelY, accelZ, gyroX, gyroY, gyroZ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb = new DatabaseHelper(this);
//Assign TextViews to specific axises
xText = (TextView) findViewById(R.id.xText);
yText = (TextView) findViewById(R.id.yText);
zText = (TextView) findViewById(R.id.zText);
xTextGyro = (TextView) findViewById(R.id.xTextGyro);
yTextGyro = (TextView) findViewById(R.id.yTextGyro);
zTextGyro = (TextView) findViewById(R.id.zTextGyro);
toggle = (ToggleButton) findViewById(R.id.toggleButton); //toggle for start/stop recording
//Creating the sensor manager; SENSOR_SERVICE is used to access sensors.
sM = (SensorManager) getSystemService(SENSOR_SERVICE);
//Accelerometer Sensor.
accelerometer = sM.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
if(accelerometer != null){
//Register sensor listener;
sM.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
Log.d(TAG, "onCreate initializing sensors");
} else{
xText.setText("Accelerometer not supported");
yText.setText("Accelerometer not supported");
zText.setText("Accelerometer not supported");
}
//GYRO Sensor.
gyroscope = sM.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
if(gyroscope != null){
//Register sensor listener;
sM.registerListener(this, gyroscope, SensorManager.SENSOR_DELAY_NORMAL);
Log.d(TAG, "onCreate initializing sensors");
} else{
xTextGyro.setText("GYROSCOPE not supported");
yTextGyro.setText("GYROSCOPE not supported");
zTextGyro.setText("GYROSCOPE not supported");
}
@Override
public void onSensorChanged(SensorEvent event) {
Sensor sensorType = event.sensor;
if(sensorType.getType()==Sensor.TYPE_ACCELEROMETER) {
xText.setText("X: " + event.values[0]);
yText.setText("Y: " + event.values[1]);
zText.setText("Z: " + event.values[2]);
accelX = event.values[0];
accelY = event.values[1];
accelZ = event.values[2];
} else if (sensorType.getType() == Sensor.TYPE_GYROSCOPE){
xTextGyro.setText("X: " + event.values[0]);
yTextGyro.setText("Y: " + event.values[1]);
zTextGyro.setText("Z: " + event.values[2]);
gyroX = event.values[0];
gyroY = event.values[1];
gyroZ = event.values[2];
}
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(toggle.isChecked()){
DatabaseHelper.getInstance().insertTable(accelX, accelY, accelZ, gyroX, gyroY, gyroZ);
Toast.makeText(MainActivity.this, "Recording data...", Toast.LENGTH_SHORT).show();
}
}
});
我的databasehelper类:
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String SENSOR_TABLE = "SENSOR_TABLE";
public static final String COLUMN_ACCEL_X = "ACCEL_X";
public static final String COLUMN_ACCEL_Y = "ACCEL_Y";
public static final String COLUMN_ACCEL_Z = "ACCEL_Z";
public static final String COLUMN_GYRO_X = "GYRO_X";
public static final String COLUMN_GYRO_Y = "GYRO_Y";
public static final String COLUMN_GYRO_Z = "GYRO_Z";
private static DatabaseHelper mInstance;
public DatabaseHelper(Context context) {
super(context, "AccelButton.db", null, 1);
SQLiteDatabase db = this.getWritableDatabase();
}
public static DatabaseHelper getInstance(){
if(mInstance == null){
synchronized (DatabaseHelper.class){
if(mInstance == null){
mInstance = new DatabaseHelper(BaseApp.getApp());
}
}
}
return mInstance;
}
@Override
public void onCreate(SQLiteDatabase db) {
//String createTableStatement= "CREATE TABLE " + SENSOR_TABLE + "( " + COLUMN_ACCEL_X + " REAL, " + COLUMN_ACCEL_Y + " REAL, " + COLUMN_ACCEL_Z + " REAL, time DATETIME DEFAULT CURRENT_TIME)";
String createTableStatement= "CREATE TABLE " + SENSOR_TABLE + "( time DATETIME DEFAULT CURRENT_TIME, " + COLUMN_ACCEL_X + " REAL, " + COLUMN_ACCEL_Y + " REAL, " + COLUMN_ACCEL_Z + " REAL, " + COLUMN_GYRO_X + " REAL, " + COLUMN_GYRO_Y + " REAL, " + COLUMN_GYRO_Z + " REAL)";
db.execSQL(createTableStatement);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+SENSOR_TABLE);
onCreate(db);
}
public void insertTable(float x, float y, float z ,float a, float b , float c){ //put onSensorChanged data to database
ContentValues contentvalues = new ContentValues();
contentvalues.put("ACCEL_X", x);
contentvalues.put("ACCEL_Y", y);
contentvalues.put("ACCEL_Z", z);
contentvalues.put("GYRO_X", a);
contentvalues.put("GYRO_Y", b);
contentvalues.put("GYRO_Z", c);
getWritableDatabase().insert(SENSOR_TABLE, null, contentvalues);
}
}
我的baseapp类:
public class BaseApp extends Application {
private static BaseApp mInstance ;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static Application getApp(){
return mInstance;
}
}
1条答案
按热度按时间06odsfpq1#
您添加的toggle监听器将报告“onchange”,因此只有在用户点击该按钮时才会报告。你应该尽可能少的做些改变。移除切换,并将其放置在其他地方,如oncreate。
设置一个标记以跟踪切换何时激活:
在onsensorchanged上,您可以:
现在应该将数据插入数据库。我建议您重构此代码,并将其移到onsensorchanged之外。