I am working in beacon type where I need to reset count for every half an hour. When the device gets triggered, I will insert data in db for corresponding date and time and counter. But when it reached half an hour, need to reset counter from 1. How to handle this?
Here is code what I tried.
This database method will be called in service class..
if(Application.getPrefString(BluetoothLeService.this,Application.ACTIVITY_LOGGING_ON).equalsIgnoreCase(getString(R.string.activity_logging_on))) {
if(GattConstant.CHAR_ACTIVITY_NOTIFY.equals(characteristic.getUuid())) {
Log.e("logging on", "logging on");
dbHelper.insertTestHistory();
}
}
DatabaseHelper:
private int activityCounts=0;
public void insertTestHistory() {
synchronized (lock) {
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss", Locale.getDefault());
String date = dateFormat.format(new Date());
previousDate = date;
Log.e("activitycount", activityCounts + " " + date);
activityCounts++;
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Application.ACTIVITY_LOG_STATUS, previousDate + ",activity," + (activityCounts));
database.insert(Application.ACTIVITY_LOG_TABLE, null, values);
database.close();
}
}
public List<String> getActivityHistory() {
synchronized (lock) {
List<String> deviceHistoryLog = new ArrayList<String>();
String selectQuery = "SELECT * FROM " + Application.ACTIVITY_LOG_TABLE + " ORDER BY ROWID DESC";
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
deviceHistoryLog.add(cursor.getString(cursor.getColumnIndex(Application.ACTIVITY_LOG_STATUS)));
} while (cursor.moveToNext());
}
// Close cursor and database.
cursor.close();
database.close();
return deviceHistoryLog;
}
}
in MainActivity:
public class MainActivity extends Activity{
Da
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tracking);
dbHelper = DatabaseHelper.getInstance(this);
deviceHistory = dbHelper.getActivityHistory();
if (deviceHistory.size() == 0) {
Log.e("empty", "empty");
} else {
for (int i = 0; i < deviceHistory.size(); i++) {
historyContent += deviceHistory.get(i);
historyData = deviceHistory.get(i);
Log.e("historydata", historyContent);
}
}
}
The data which I am getting is:
04-28 14:41:32.730 30076-30091/com.test E/activitycount: 1 04/28/2016 14:41:32
04-28 14:41:33.861 30076-30091/com.test E/logging on: logging on
04-28 14:41:33.863 30076-30091/com.test E/activitycount: 2 04/28/2016 14:41:33
04-28 14:41:33.879 30076-30091/com.test E/logging on: logging on
04-28 14:41:33.881 30076-30091/com.test E/activitycount: 3 04/28/2016 14:41:33
04-28 14:41:33.895 30076-30091/com.test E/logging on: logging on
04-28 14:41:33.896 30076-30091/com.test E/activitycount: 4 04/28/2016 14:41:33
I need to reset when it reached 15:01 ie activity logging to 1 when it triggers.. then 15.02 as 2 and so on and in every half an hour, counter to reset.