Android - Update TextView periodically

2019-02-25 18:43发布

I simply want to update a TextView periodically by a service. Let's say the TextView should display the time and update itself every 30 seconds. Could't you guys tell me how I can achieve that? I know that I need a receiver for that but everything I tried did't work at all.

I'm new to android, maybe you can keep it simple :D

2条回答
三岁会撩人
2楼-- · 2019-02-25 19:19
Handler h = new Handler();
int delay = 30000; //milliseconds

h.postDelayed(new Runnable(){
    public void run(){
        editText.setText(java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime())+"");
        h.postDelayed(this, delay);
    }
}, delay);
查看更多
乱世女痞
3楼-- · 2019-02-25 19:23

In onCreate() //updating every 30 seconds

      Handler handler =new Handler();
      final Runnable r = new Runnable() {
                public void run() {
                    handler.postDelayed(this, 30000);
                    String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
                    edittextId.setText(mydate);
                }
            };
            handler.postDelayed(r, 0000);
查看更多
登录 后发表回答