首页 > 解决方案 > 在 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>

标签: androidfirebasetimer

解决方案


每辆自行车必须有一个id这样的id

存储userStartTime在火力基地

将文件存储userEndTime在 firebase 中,然后让经理存储cycleStandEndTime.

然后,您在 firebase 的 3 个字段中拥有所有数据,您可以进行任何您想要的计算。

totalUserUseTime = userEndTime - userStartTime

totalBikeRentalTime = cycleStandEndTime - userStartTime


推荐阅读