To jest stara wersja strony!


Dostęp do danych GPS w Androidzie

Ambient Profile

Zintegruj program GPS Tracker z projektem GUI Ambient Profile, tak aby:

  • 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)
  • Zadanie dodatkowe Trzeci fragment (Map) wyświetlać powinien mapę z ikonami profili w obszarach ich aktywacji

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.

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

GPS

Poniżej klasa reprezentująca rekord danych.

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

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.

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

GPSDataSource

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> comments = new ArrayList<GPS>();
 
    Cursor cursor = database.query(DataBaseHelper.TABLE_GPS,
        allColumns, null, null, null, null, null);
 
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
      GPS gps = cursorToGPS(cursor);
      comments.add(gps);
      cursor.moveToNext();
    }
    // Make sure to close the cursor
    cursor.close();
    return comments;
  }
 
  private GPS cursorToGPS(Cursor cursor) {
    GPS gps = null;
    if (cursor != null) {
      if (cursor.moveToFirst()) {
         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;
  }
} 

Geopłoty

pl/dydaktyka/aml/lab3.1382689930.txt.gz · ostatnio zmienione: 2019/06/27 15:51 (edycja zewnętrzna)
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0