【问题标题】:how to set a timer to update data in firebase database如何设置计时器以更新firebase数据库中的数据
【发布时间】:2019-02-13 11:22:57
【问题描述】:

所以基本上我正在构建一个应用程序,它将在屏幕上显示事件并在达到设置的特定时间后立即更新它。使用计时器而不是 Cloud Functions 来实现这一点会更容易,但是当我尝试时,它就是行不通。查看 StackOverFlow 上的其他问题,但都不清楚。我究竟做错了什么?下面是我的代码。

update_btn.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {

    int time = 0;

    String info = information.getEditText().getText().toString();
    final long timeCountInMilliSeconds = time * 60 * 1000;
    time = Integer.parseInt(xxx.getEditText().getText().toString().trim());

    if (!TextUtils.isEmpty(info) && !TextUtils.isEmpty(String.valueOf(time))) {

        progressBar.setVisibility(View.VISIBLE);

        update_ref = FirebaseDatabase.getInstance().getReference().child("Event Halls").child("Ankobra One").child("Event Schedule");
        final int finalTime = time;
        update_ref.setValue(info).addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                final DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Event Halls").child("Ankobra One").child("Event Schedule");
                ref.addListenerForSingleValueEvent(new ValueEventListener() {
                    void onDataChanged(@NonNull DataSnapshot snapshot) {
                        new android.os.Handler().postDelayed(
                                new Runnable() {
                                    public void run() {
                                        retrieve_ref.setValue("No event scheduled");
                                    }
                                },
                                timeCountInMilliSeconds);
                    }

                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });
                        Toast.makeText(AnkobraOneActivity.this, "Event Changed and scheduled for " + finalTime + " hours", Toast.LENGTH_LONG).show();
                        progressBar.setVisibility(View.INVISIBLE);
                        myDialog.dismiss();
                    }
                });

            } else

            {
                Toast.makeText(AnkobraOneActivity.this, "Insert all details accordingly", Toast.LENGTH_SHORT).show();
            }
        }

    });

我使用这个答案来实现我到目前为止所做的事情,但它没有奏效。

https://stackoverflow.com/questions/4817933/what-is-the-equivalent-to-a-javascript-setinterval-settimeout-in-android-java

帮助?

【问题讨论】:

  • 如果您想在特定时间后更新您的 firebase 数据库,请在处理程序中执行 setValue(),并在 postdelay 以毫秒为单位传递您的时间间隔
  • 嗨@kampangala 我必须检索用户选择的时间,这就是为什么我不能指定毫秒。要么这个,要么我没有得到你的答案。
  • 您传递给处理程序的timeCountInMilliseconds 始终是0,因为您在实际检索用户输入之前设置了它。

标签: java android firebase-realtime-database timer


【解决方案1】:

根据您的代码,

   int time = 0;
   String info = information.getEditText().getText().toString();
   final long timeCountInMilliSeconds = time * 60 * 1000;

.

timeCountInMilliSeconds 

总是 =0;

所以,你必须将代码更改为

  int time = 0;
  String info = information.getEditText().getText().toString();
  time =Integer.parseInt(xxx.getEditText().getText().toString().trim());
  final long timeCountInMilliSeconds = time * 60 * 1000;

另外,你必须在定时器方法中使用setValue()

完整代码如下:

update_btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        int time = Integer.parseInt(xxx.getEditText().getText().toString().trim());
                        String info = information.getEditText().getText().toString();
                        final long timeCountInMilliSeconds = time * 60 * 1000;

                        if (!TextUtils.isEmpty(info) && !TextUtils.isEmpty(String.valueOf(time))) {

                            progressBar.setVisibility(View.VISIBLE);

                new Timer().scheduleAtFixedRate(new TimerTask() {

                            @Override
                        public void run() {
                            //Called each time when 1000 milliseconds (1 second) (the period parameter)

                            update_ref = 
            FirebaseDatabase.getInstance().getReference().child("Event Halls").child("Ankobra One").child("Event Schedule");
                            final int finalTime = time;
                            update_ref.setValue(info).addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                        final DatabaseReference ref = 
            FirebaseDatabase.getInstance().getReference().child("Event Halls").child("Ankobra One").child("Event Schedule");
                                    ref.addListenerForSingleValueEvent(new ValueEventListener() {
                                        void onDataChanged(@NonNull DataSnapshot snapshot) {
                                              retrieve_ref.setValue("No event scheduled");


                                        }

                                        @Override
                                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                                        }

                                        @Override
                                        public void onCancelled(@NonNull DatabaseError databaseError) {

                                        }
                                    });
                                            Toast.makeText(AnkobraOneActivity.this, "Event Changed and scheduled for " + finalTime + " hours", Toast.LENGTH_LONG).show();
                                            progressBar.setVisibility(View.INVISIBLE);
                                            myDialog.dismiss();
                                        }
                                    });
                              },
                    //Set how long before to start calling the TimerTask (in milliseconds)
                    0,
                    //Set the amount of time between each execution (in milliseconds)
                    timeCountInMilliSeconds);
                                } else

                                {
                                    Toast.makeText(AnkobraOneActivity.this, "Insert all details accordingly", Toast.LENGTH_SHORT).show();
                                }
                            }

                        });

【讨论】:

    猜你喜欢
    • 2022-07-08
    • 1970-01-01
    • 2020-01-07
    • 2021-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-18
    • 1970-01-01
    相关资源
    最近更新 更多