Update TextView Every Second

前端 未结 9 1270
南旧
南旧 2020-11-28 23:58

I\'ve looked around and nothing seems to be working from what I\'ve tried so far...

    @Override
protected void onCreate(Bundle savedInstanceState) {
    su         


        
相关标签:
9条回答
  • 2020-11-29 00:25

    You can also use TimerTask for that.

    Here is an method

    private void setTimerTask() {
        long delay = 3000;
        long periodToRepeat = 60 * 1000; /* 1 mint */
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                     //   do your stuff here.
                    }
                });
            }
        }, 3000, 3000);
    }
    
    0 讨论(0)
  • 2020-11-29 00:27

    This Code work for me..

    //Get Time and Date
    private String getTimeMethod(String formate)
    {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(formate);
        String formattedDate= dateFormat.format(date);
        return formattedDate;
    }
    
    //this method is used to refresh Time every Second
    private void refreshTime() //Call this method to refresh time
    {
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        txtV_Time.setText(getTimeMethod("hh:mm:ss a")); //hours,Min and Second with am/pm
                        txtV_Date.setText(getTimeMethod("dd-MMM-yy")); //You have to pass your DateFormate in getTimeMethod()          
                    };
                });
            }
        }, 0, 1000);//1000 is a Refreshing Time (1second)
    }
    
    0 讨论(0)
  • 2020-11-29 00:30

    It would be better if you just used an AsyncTask running using a Timer something like this

     Timer LAIATT = new Timer();
        TimerTask LAIATTT = new TimerTask()
        {
            @Override
            public void run()
            {
                LoadAccountInformationAsyncTask LAIAT = new LoadAccountInformationAsyncTask();
                LAIAT.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            }
        };
        LAIATT.schedule(LAIATTT, 0, 1000);
    
    0 讨论(0)
提交回复
热议问题