Both sides previous revision
Poprzednia wersja
Nowa wersja
|
Poprzednia wersja
|
pl:dydaktyka:aml:lab3 [2013/10/25 09:53] esimon [Ambient Profile] |
pl:dydaktyka:aml:lab3 [2019/06/27 15:50] (aktualna) |
====== Dostęp do danych GPS w Androidzie ====== | ====== Dostęp do danych GPS w Androidzie ====== |
===== Ambient Profile ===== | ===== Ambient Profile ===== |
Zintegruj program [[https://sbobek@bitbucket.org/sbobek/aml_gpstracker|GPS Tracker]] z projektem GUI [[https://sbobek@bitbucket.org/sbobek/aml_ambientprofile|Ambient Profile]], tak aby: | Zintegruj program [[https://sbobek@bitbucket.org/sbobek/aml_gpstracker_studio|GPS Tracker]] z projektem GUI [[https://sbobek@bitbucket.org/sbobek/aml_ambientprofile|Ambient Profile]], tak aby: |
| |
{{:pl:dydaktyka:aml:ambient-profile.png?300 |}} | {{:pl:dydaktyka:aml:ambient-profile.png?300 |}} |
* Pierwszy fragment (**Status**) umożliwiał włączanie i wyłączanie usługi śledzenia i wyświetlał aktywne profile przy pomocy ListView | * Pierwszy fragment (**Status**) umożliwiał włączanie i wyłączanie usługi śledzenia. |
* Drugi fragment (**Profiles**) wyświetlał listę profili jakie użytkownik aktualnie ma. Dodaj przycisk i aktywność (FragmentDialog) umożliwiający dodanie nowego profilu. Zaprojektuj klasę reprezentującą profil, przechowującą różne rodzaje profilu (nazwa, co ma się dziać kiedy profil jest aktywny) | * Drugi fragment (**Profiles**) wyświetlał listę profili jakie użytkownik aktualnie ma. Dodaj przycisk i aktywność (FragmentDialog) umożliwiający dodanie nowego profilu. **Zaprojektuj** klasę reprezentującą profil, przechowującą różne rodzaje profilu (nazwa, co ma się dziać kiedy profil jest aktywny) |
| * Przetestuj działanie aplikacji za pomocą DDMS |
* **Zadanie dodatkowe** Trzeci fragment (Map) wyświetlać powinien mapę z ikonami profili w obszarach ich aktywacji | * **Zadanie dodatkowe** Trzeci fragment (Map) wyświetlać powinien mapę z ikonami profili w obszarach ich aktywacji |
| * **Zadanie dodatkowe** Do detekcji tego czy ktoś pojawił się w obszarze aktywacji danego profilu wykorzystaj //geopłoty// [[http://developer.android.com/training/location/geofencing.html|Geofences]] |
| |
===== Baza danych ===== | ===== Baza danych ===== |
| 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: |
| <code java> |
| //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(); |
| </code> |
| ==== GPS ==== |
| Poniżej klasa reprezentująca rekord danych. |
| <file java GPS.java> |
| 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; } |
| |
| } |
| |
| </file> |
| |
| ==== DataBaseHelper ==== |
| 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//. |
| <file java DataBaseHelper.java> |
| 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); |
| } |
| |
| } |
| |
| </file> |
| |
| |
| ==== GPSDataSource ==== |
| Klasa opakowuje klasę //DataBaseHelper// udostępniając interfejs dedykowany dla operacji na obiektach klasy //GPS//. |
| |
| <file java GPSDataSource.java> |
| 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; |
| } |
| } |
| </file> |
| |
| |
| |
| |
===== Geopłoty ===== | |
| |
| |
| |
| |