Zintegruj program GPS Tracker z projektem GUI Ambient Profile, tak aby:
Dodaj do projektu klasy odpowiedzialne za zapisywanie i odczyt profili użytkownika z i do bazy SQLite. Poniżej znajdują się przykłady takich klas do przechowywania danych GPS. Zmodyfikuj je tak aby umożliwiały przechowywanie profili zaprojektowanych w poprzednim ćwiczeniu.
Przykładowe wykorzystanie klas poniżej:
//W onCreate tworzymy uchwyt do źródła danych GPSDataSource datasource = new GPSDataSource(getBaseContext()); datasource.open(); //Gdzieś dalej w kodzie wrzucamy dane do bazy: if(datasource != null){ datasource.createGPSEntry( currentLocation.getLatitude(), currentLocation.getLongitude(), Long.toString(currentTime), refreshFrequency, batteryLevel ); //Zamykamy źródło danych w onStop albo onDestroy datasource.close();
Poniżej klasa reprezentująca rekord danych.
public class GPS { private long id; private double lat; private double lon; private String timestamp; private int frequency; private int battery; double getLat() { return lat;} void setLat(double lat) { this.lat = lat; } double getLon() { return lon;} void setLon(double lon) { this.lon = lon;} String getTimestamp() { return timestamp; } void setTimestamp(String timestamp) { this.timestamp = timestamp; } public long getId() { return id; } public void setId(long id) { this.id = id; } public int getBattery() { return battery; } public void setBattery(int battery) { this.battery = battery; } public int getFrequency() { return frequency; } public void setFrequency(int frequency) {this.frequency = frequency; } }
Poniżej klasa służąca za uchwyt do bazy danych. Tworzy bazę danych i umożliwia operację na tabelach i danych. Jest wykorzystywana przez klasę GPSDataSource.
import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class DataBaseHelper extends SQLiteOpenHelper { public static final String TABLE_GPS = "gps"; public static final String COLUMN_ID = "_id"; public static final String COLUMN_LAT = "lat"; public static final String COLUMN_LON = "lon"; public static final String COLUMN_TIME = "timestamp"; public static final String COLUMN_FREQUENCY = "frequency"; public static final String COLUMN_BATTERY_LEVEL = "battery"; public static final String DATABASE_NAME = "gps.db"; private static final int DATABASE_VERSION = 1; // Database creation sql statement private static final String DATABASE_CREATE = "create table " + TABLE_GPS + "(" + COLUMN_ID + " integer primary key autoincrement, " + COLUMN_LAT + " double not null, " + COLUMN_LON + " double not null, " + COLUMN_TIME + " text not null, " + COLUMN_FREQUENCY + " int not null," + COLUMN_BATTERY_LEVEL + " integer default 0);"; public DataBaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase database) { database.execSQL(DATABASE_CREATE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.w(DataBaseHelper.class.getName(), "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data"); db.execSQL("DROP TABLE IF EXISTS " + TABLE_GPS); onCreate(db); } }
Klasa opakowuje klasę DataBaseHelper udostępniając interfejs dedykowany dla operacji na obiektach klasy GPS.
import java.util.ArrayList; import java.util.List; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; public class GPSDataSource { // Database fields private SQLiteDatabase database; private DataBaseHelper dbHelper; private String[] allColumns = { DataBaseHelper.COLUMN_ID, DataBaseHelper.COLUMN_LAT, DataBaseHelper.COLUMN_LON, DataBaseHelper.COLUMN_TIME, DataBaseHelper.COLUMN_FREQUENCY, DataBaseHelper.COLUMN_BATTERY_LEVEL}; public GPSDataSource(Context context) { dbHelper = new DataBaseHelper(context); } public void open() throws SQLException { database = dbHelper.getWritableDatabase(); } public void close() { dbHelper.close(); } public GPS createGPSEntry(double lat, double lon, String timestamp, int frequency, int battery) { ContentValues values = new ContentValues(); values.put(DataBaseHelper.COLUMN_LAT, lat); values.put(DataBaseHelper.COLUMN_LON, lon); values.put(DataBaseHelper.COLUMN_TIME, timestamp); values.put(DataBaseHelper.COLUMN_LON, lon); values.put(DataBaseHelper.COLUMN_FREQUENCY, frequency); values.put(DataBaseHelper.COLUMN_BATTERY_LEVEL, battery); long insertId = database.insert(DataBaseHelper.TABLE_GPS, null, values); Cursor cursor = database.query(DataBaseHelper.TABLE_GPS, allColumns, DataBaseHelper.COLUMN_ID + " = " + insertId, null, null, null, null); cursor.moveToFirst(); GPS newGPS = cursorToGPS(cursor); cursor.close(); return newGPS; } public void deleteGPS(GPS gps) { long id = gps.getId(); database.delete(DataBaseHelper.TABLE_GPS, DataBaseHelper.COLUMN_ID + " = " + id, null); } public List<GPS> getAllGPS() { List<GPS> gpses = new ArrayList<GPS>(); Cursor cursor = database.query(DataBaseHelper.TABLE_GPS, allColumns, null, null, null, null, null); if(cursor.moveToFirst()){ while (!cursor.isAfterLast()) { GPS gps = cursorToGPS(cursor); gpses.add(gps); cursor.moveToNext(); } } // Make sure to close the cursor cursor.close(); return gpses; } private GPS cursorToGPS(Cursor cursor) { GPS gps = null; if (cursor != null) { gps = new GPS(); gps.setId(cursor.getLong(0)); gps.setLat(cursor.getDouble(cursor.getColumnIndex(DataBaseHelper.COLUMN_LAT))); gps.setLon(cursor.getDouble(cursor.getColumnIndex(DataBaseHelper.COLUMN_LON))); gps.setTimestamp(cursor.getString(cursor.getColumnIndex(DataBaseHelper.COLUMN_TIME))); gps.setFrequency(cursor.getInt(cursor.getColumnIndex(DataBaseHelper.COLUMN_FREQUENCY))); gps.setBattery(cursor.getInt(cursor.getColumnIndex(DataBaseHelper.COLUMN_BATTERY_LEVEL))); } return gps; } }