android - 在 Firebase 中实现 CountUp 计时器
问题描述
我正在开发一个用于租用自行车的 android 应用程序,它使用 firebase 数据库(实时)和谷歌地图。
现在我想添加一个 CountUp 计时器(采用 HH:mm:ss 格式)来跟踪服务器中的骑行时间。
为什么有这个要求?
因为一旦成功预订骑行,骑行时间将由客户开始,但只有在自行车归还时,由其应用程序在自行车站管理自行车的人才能结束。(为确保自行车已安全归还)
如何在 Firebase 中实现此计时器?
到目前为止,我只在客户端实现了一个 CountUp Timer,它使用广播接收器来跟踪后台时间。
解决此问题的一种方法可能是,客户结束行程(仅当客户在自行车站附近 20 米时),同时将其存储到火力基地,并由管理自行车站的人员访问。
但是这种方法有一些缺点:
如果一个人从外站结束行程并在一个小时后返回,计算的时间将是错误的,也会影响下一个用户的可用性状态。
//Service Class
public class BroadcastService extends Service {
private Intent intent;
public static final String BROADCAST_ACTION = "com.xyz";
private Handler handler = new Handler();
private long initial_time;
long timeInMilliseconds = 0L;
@Override
public void onCreate() {
super.onCreate();
initial_time = SystemClock.uptimeMillis();
intent = new Intent(BROADCAST_ACTION);
handler.removeCallbacks(sendUpdatesToUI);
handler.postDelayed(sendUpdatesToUI, 1000); // 1 second
}
private Runnable sendUpdatesToUI = new Runnable() {
public void run() {
DisplayLoggingInfo();
handler.postDelayed(this, 1000); // 1 seconds
}
};
private void DisplayLoggingInfo() {
timeInMilliseconds = SystemClock.uptimeMillis() - initial_time;
int timer = (int) timeInMilliseconds / 1000;
intent.putExtra("time", timer);
sendBroadcast(intent);
}
@Override
public void onDestroy() {
super.onDestroy();
handler.removeCallbacks(sendUpdatesToUI);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
// Java class which runs when ride is active and shows the timer
public class RideIsActive extends AppCompatActivity {
private TextView timer;
private Button endRide;
Intent intent;
long timeSwapBuff = 0L;
long updatedTime = 0L;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ride_is_active);
timer = findViewById(R.id.timer);
endRide = findViewById(R.id.endRide);
startService(new Intent(this, BroadcastService.class));
endRide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
unregisterReceiver(broadcastReceiver);
stopService(intent);
}
});
}
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
updateUI(intent);
}
};
private void updateUI(Intent intent) {
int time = intent.getIntExtra("time", 0);
Log.d("Hello", "Time " + time);
int hrs = time/3600;
int mins = time / 60;
int secs = time % 60;
timer.setText(String.format("%02d", hrs) + ":" + String.format("%02d", mins) + ":"
+ String.format("%02d", secs));
}
@Override
protected void onResume() {
super.onResume();
registerReceiver(broadcastReceiver, new IntentFilter(BroadcastService.BROADCAST_ACTION));
}
//Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RideIsActive">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/timer"
android:layout_centerInParent="true"
android:textSize="25dp"
android:textColor="#000000"
android:text="00:00:00"/>
<Button
android:id="@+id/endRide"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="End Ride"
android:textColor="@color/textBlack"
android:background="@color/quantum_orange500"/>
</RelativeLayout>
解决方案
每辆自行车必须有一个id
这样的id
:
存储userStartTime
在火力基地
将文件存储userEndTime
在 firebase 中,然后让经理存储cycleStandEndTime
.
然后,您在 firebase 的 3 个字段中拥有所有数据,您可以进行任何您想要的计算。
totalUserUseTime = userEndTime - userStartTime
totalBikeRentalTime = cycleStandEndTime - userStartTime
推荐阅读
- javascript - 如何将 D3.js GeoJSON 和 CSV 加载到 CodeIgniter
- python - 为什么numpy数组不会将float更改为int?
- java - Apache ActiveMQ Artemis 问题将 Windows 客户端连接到 Linux 服务器
- javascript - 如何在材质ui中具有一致的宽度
- javascript - 获取值输入单选按钮javascript
- python - 使用python eel的PHP sqlite3数据库下拉菜单
- javascript - 如何使用 Vuejs 前端框架使用 MySQL/PHP 从服务器获取数据
- c# - asp.net core中的Host和WebHost类有什么区别
- javascript - Antd Table 没有重新渲染
- ios - 4.1 版的 Swift 快照