vendredi 15 janvier 2016

How to make my SQLite Database insert data every x second?

I am currently doing a project using Microsoft Band. I want to record the heart rate from the Microsoft Band into a SQLite Database every 20 seconds with timestamp. Anyone know how to do it?

Here is my code to retrieve the heart rate from the Microsoft Band:

    private BandHeartRateEventListener mHeartRateEventListener = new BandHeartRateEventListener() {
    @Override
    public void onBandHeartRateChanged(final BandHeartRateEvent event) {
        int heart = event.getHeartRate(); //heart rate value
        if (event != null) {
            appendToHeart(String.format("Heart Rate = %d beats per minute\n"
                    + "Quality = %s\n", heart, event.getQuality()));
            if(heart>80||heart<60){
                double latitude = gps.getLatitude();
                double longtitude = gps.getLongtitude();
                String latitude2 = Double.toString(latitude);
                String longtitude2 = Double.toString(longtitude);
                String phone_number = "12345678";
                String text = "Your Location is -\nLat: " + latitude2 + "\nLong: " + longtitude2+"\n User have dangerous heart rate!!!";
                SmsManager smsMgr = SmsManager.getDefault();
                smsMgr.sendTextMessage(phone_number, null, text, null, null);
            }


        }


    }

};

Here is the code of my database:

public class DatabaseHelper extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "PulseData.db";
private static final String TABLE_NAME = "PulseData";
private static final String COLUMN_ID = "id";
private static final String COLUMN_TIMESTAMP = "timestamp";
private static final String COLUMN_PULSE = "pulse";

SQLiteDatabase db;

private static final String TABLE_CREATE = "create table information (id integer primary key not null , " +
        "timestamp text not null , pulse text not null);";


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

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(TABLE_CREATE);
    this.db = db;
}

public void insertPulseData(PulseData pulseData){
    db = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    String query = "select * from PulseData";
    Cursor cursor = db.rawQuery(query, null);
    int count = cursor.getCount();

    values.put(COLUMN_ID, count);
    values.put(COLUMN_TIMESTAMP, pulseData.getTimestamp());
    values.put(COLUMN_PULSE, pulseData.getPulse());

    db.insert(TABLE_NAME, null, values);
    db.close();
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    String query = "DROP TABLE IF EXISTS " + TABLE_NAME;
    db.execSQL(query);
    this.onCreate(db);
}

}

Here is part of my heartrate.xml code whereby when I click on a button it leads to a history page and display the history record of heart rate retrieve from the SQLite Database.

    public void onHeartrateClick(View v){
    if(v.getId() == R.id.Bhistory){
        int heart = event.getHeartRate();
        String.valueOf(heart);
    }
} 

When I do up to this part I am lost. Any idea what should I do next?

Aucun commentaire:

Enregistrer un commentaire