我在信标类型工作,我需要每半小时重置一次计数。当设备被触发时,我将在db中插入相应日期和时间以及计数器的数据。但是当它达到半小时后,需要从1重置计数器。如何处理?
以下是我尝试的代码。
此数据库方法将在服务类中调用..
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);
}
}
}
我得到的数据是:
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
当它达到15:01时我需要重置,即触发时活动记录为1。 然后15.02为2,依此类推,每半小时一次,反击重置。
答案 0 :(得分:1)
您可以使用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);
并使用广播接收器更新计数
public class NotificationBroadcast extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
....
}
}
答案 1 :(得分:0)
<强> 1 即可。维持上一个重置时间的类级字段,以毫秒为单位
long lastResetTimeInMilliSecs = new Date()。getTime()/ 1000;
制作一个计时器,以便在30分钟后重置activityCounts
<div class="container">
<div class="row">
<div class="col-sm-6">
<!-- left column -->
</div>
<div class="col-sm-6">
<!-- right column -->
</div>
</div>
</div>
}
您的方法是这样的,不要将计时器逻辑与此方法混合
<div class="container">
<div class="row">
<div class="col-sm-6">
<!-- left column -->
<div class="row"><!-- Nested Row. Children columns add up to 12 -->
<div class="col-sm-7">
7/12 of the left side
</div>
<div class="col-sm-5">
5/12 of the left side
</div>
</div>
</div>
<div class="col-sm-6">
<!-- right column -->
</div>
</div>
</div>
启动计时器一次,即调用startResetTimer方法一次,每次调用insertTestHistory时都不需要调用它