1

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.

PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
Shadow
  • 6,864
  • 6
  • 44
  • 93

2 Answers2

1

You can use AlarmManager

AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent notifyintent = new Intent(this, NotificationBroadcast.class);
pIntent = PendingIntent.getBroadcast(this, 0, notifyintent, 0);

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
//update 30 seconds
alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),1*30*1000, pIntent);

and update count using broadcast receiver

public class NotificationBroadcast extends BroadcastReceiver {  

    @Override
    public void onReceive(final Context context, final Intent intent) {
       ....
   }
}
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
Parthi
  • 669
  • 1
  • 9
  • 26
0

1. Maintain a class level field for last reset time in milliSeconds

long lastResetTimeInMilliSecs = new Date ().getTime() / 1000;

  1. Make a timer to reset activityCounts after 30minutes

    public void startResetTimer() {
      new Timer ().schedule (new TimerTask () {
       @Override
       public void run () {
    
        ((BaseActivity) context).runOnUiThread (new Runnable () {
            @Override
            public void run () {
    
                try {
                    long differenceInSeconds = (new Date().getTime() - lastResetTime) / 1000;  //divide by 1000 to get time in seconds
                    if(differenceInSeconds >= 30 * 60 * differenceInSeconds) {  //30 mins = hours (multiple by 60 to convert into minutes) = 30minutes
                        //resetCounter;
                        activityCounts = 0;
                        //update last reset time 
                        lastResetTime = new Date ().getTime();
                    }
                }
                catch (Exception e) {
                    e.printStackTrace ();
                }
            }
        });
    }
    
    }, 0, 1000);
    

    }

  2. Your method is like this, don't mix timer logic with this method

    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(VALRTApplication.ACTIVITY_LOG_STATUS, previousDate + ",activity," + (activityCounts));
        database.insert(VALRTApplication.ACTIVITY_LOG_TABLE, null, values);
        database.close();
      }
    }
    
  3. just start timer once, i.e. call startResetTimer method once, no need to call it every time insertTestHistory is called

PEHLAJ
  • 9,980
  • 9
  • 41
  • 53