package cici.sensor.test; import java.util.List; import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.app.Activity; import android.hardware.SensorListener; import android.hardware.SensorManager; import android.os.Bundle; import android.util.Log; import android.widget.LinearLayout; import android.widget.TextView; public class SensorActivity extends Activity implements SensorListener { /** Called when the activity is first created. */ final String tag = "Sensor Test"; SensorManager sm = null; TextView xViewA = null; TextView yViewA = null; TextView zViewA = null; TextView xViewO = null; TextView yViewO = null; TextView zViewO = null; private static final String DATABASE_NAME = "SensorData.db"; private static final int DATABASE_VERSION = 1; private static final String TABLE_NAME = "SensorData"; private static final String DirectionX = "dx"; private static final String DirectionY = "dy"; private static final String DirectionZ = "dz"; private static final String AccelerationX = "ax"; private static final String AccelerationY = "ay"; private static final String AccelerationZ = "az"; DatabaseHelper mOpenHelper; private static class DatabaseHelper extends SQLiteOpenHelper { DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { System.out.println("11111"); // String sql = "CREATE TABLE" + TABLE_NAME + "("+ DirectionX + " text not null, "+ DirectionY + " text not null, " + DirectionZ + " text not null, "+ AccelerationX + " text not null, " // + AccelerationY + " text not null, " + AccelerationZ + " text not null"+ ");"; String sql = "CREATE TABLE " + TABLE_NAME + " (" + DirectionX + " text not null " + ");"; try { db.execSQL("DROP TABLE IF EXISTS SensorData"); db.execSQL(sql); System.out.println("数据表成功重建"); } catch (SQLException e) { System.out.println("数据表重建错误"); } } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } } private void CreateTable() { SQLiteDatabase db = mOpenHelper.getWritableDatabase(); // String sql = "CREATE TABLE" + TABLE_NAME + "("+ DirectionX + " text not null, "+ DirectionY + " text not null, " + DirectionZ + " text not null, "+ AccelerationX + " text not null, " // + AccelerationY + " text not null, " + AccelerationZ + " text not null"+ ");"; String sql = "CREATE TABLE " + TABLE_NAME + " (" + DirectionX + " text not null " + ");"; try { db.execSQL("DROP TABLE IF EXISTS SensorData"); db.execSQL(sql); setTitle("数据表成功重建"); } catch (SQLException e) { setTitle("数据表重建错误"); } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sensor); sm = (SensorManager) getSystemService(SENSOR_SERVICE); xViewA = (TextView) findViewById(R.id.xboxa); yViewA = (TextView) findViewById(R.id.yboxa); zViewA = (TextView) findViewById(R.id.zboxa); xViewO = (TextView) findViewById(R.id.xboxo); yViewO = (TextView) findViewById(R.id.yboxo); zViewO = (TextView) findViewById(R.id.zboxo); mOpenHelper = new DatabaseHelper(this); //SQLiteDatabase db = mOpenHelper.getWritableDatabase(); CreateTable(); } public void onSensorChanged(int sensor, float[] values){ synchronized(this){ //Log.d(tag, "onSensorChanged:" + sensor +", x: " + values[0] + ", y: " + values[1] + ", z: "+ values[2]); if(sensor == SensorManager.SENSOR_ORIENTATION){ xViewO.setText("Orientation X: " + values[0]); yViewO.setText("Orientation Y: " + values[1]); zViewO.setText("Orientation Z: " + values[2]); //向数据库里输入方向上的变化 } if(sensor == SensorManager.SENSOR_ACCELEROMETER){ xViewA.setText("Accel X: " + values[0]); yViewA.setText("Accel Y: " + values[1]); zViewA.setText("Accel Z: " + values[2]); //加速度 } System.out.println("输入之前"); insertItem(); } } public void onAccuracyChanged(int sensor, int accuracy){ Log.d(tag,"onAccuracyChanged: " + sensor + ", accuracy: " + accuracy); } @Override protected void onResume(){ super.onResume(); sm.registerListener(this, SensorManager.SENSOR_ORIENTATION|SensorManager.SENSOR_ACCELEROMETER,SensorManager.SENSOR_DELAY_NORMAL); } @Override protected void onStop(){ sm.unregisterListener(this); super.onStop(); } private void insertItem() { System.out.println("进入插入函数"); SQLiteDatabase db = mOpenHelper.getWritableDatabase(); System.out.println("插入语句之前"); String sql1 = "insert into " + TABLE_NAME + " (" + DirectionX + ") values('"+ xViewA.getText().toString()+ "');"; // String sql1 = "insert into " + TABLE_NAME + " (" + TITLE + ") values('haiyang', 'android的发展真是迅速啊');"; //String sql1 = "insert into " + TABLE_NAME + " (" + DirectionX + ") values('haiyang');"; db.execSQL(sql1); } }