天天看點

傳感器資料與mysql_Android傳感器和資料庫的簡單實作

packagecici.sensor.test;importjava.util.List;importandroid.content.Context;importandroid.database.Cursor;importandroid.database.SQLException;importandroid.database.sqlite.SQLiteDatabase;importandroid.database.sqlite.SQLiteOpenHelper;importandroid.app.Activity;importandroid.hardware.SensorListener;importandroid.hardware.SensorManager;importandroid.os.Bundle;importandroid.util.Log;importandroid.widget.LinearLayout;importandroid.widget.TextView;public class SensorActivity extends Activity implementsSensorListener {

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 extendsSQLiteOpenHelper {

DatabaseHelper(Context context) {super(context, DATABASE_NAME, null, DATABASE_VERSION);

}

@Overridepublic voidonCreate(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("資料表重建錯誤");

}

}

@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, intnewVersion) {

}

}private voidCreateTable() {

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("資料表重建錯誤");

}

}

@Overridepublic voidonCreate(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, intaccuracy){

Log.d(tag,"onAccuracyChanged: " + sensor + ", accuracy: " +accuracy);

}

@Overrideprotected voidonResume(){super.onResume();

sm.registerListener(this, SensorManager.SENSOR_ORIENTATION|SensorManager.SENSOR_ACCELEROMETER,SensorManager.SENSOR_DELAY_NORMAL);

}

@Overrideprotected voidonStop(){

sm.unregisterListener(this);super.onStop();

}private voidinsertItem() {

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);

}

}