How to set timer in android?

后端 未结 21 927
渐次进展
渐次进展 2020-11-22 00:51

Can someone give a simple example of updating a textfield every second or so?

I want to make a flying ball and need to calculate/update the ball coordinates every se

相关标签:
21条回答
  • 2020-11-22 01:28

    You want your UI updates to happen in the already-existent UI thread.

    The best way is to use a Handler that uses postDelayed to run a Runnable after a delay (each run schedules the next); clear the callback with removeCallbacks.

    You're already looking in the right place, so look at it again, perhaps clarify why that code sample isn't what you want. (See also the identical article at Updating the UI from a Timer).

    0 讨论(0)
  • 2020-11-22 01:29

    You can also use an animator for it:

    int secondsToRun = 999;
    
    ValueAnimator timer = ValueAnimator.ofInt(secondsToRun);
    timer.setDuration(secondsToRun * 1000).setInterpolator(new LinearInterpolator());
    timer.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
        {
            @Override
            public void onAnimationUpdate(ValueAnimator animation)
            {
                int elapsedSeconds = (int) animation.getAnimatedValue();
                int minutes = elapsedSeconds / 60;
                int seconds = elapsedSeconds % 60;
    
                textView.setText(String.format("%d:%02d", minutes, seconds));
            }
        });
    timer.start();
    
    0 讨论(0)
  • 2020-11-22 01:30
    void method(boolean u,int max)
    {
        uu=u;
        maxi=max;
        if (uu==true)
        { 
            CountDownTimer uy = new CountDownTimer(maxi, 1000) 
      {
                public void onFinish()
                {
                    text.setText("Finish"); 
                }
    
                @Override
                public void onTick(long l) {
                    String currentTimeString=DateFormat.getTimeInstance().format(new Date());
                    text.setText(currentTimeString);
                }
            }.start();
        }
    
        else{text.setText("Stop ");
    }
    
    0 讨论(0)
  • 2020-11-22 01:31

    If you have delta time already.

    public class Timer {
        private float lastFrameChanged;
        private float frameDuration;
        private Runnable r;
    
        public Timer(float frameDuration, Runnable r) {
            this.frameDuration = frameDuration;
            this.lastFrameChanged = 0;
            this.r = r;
        }
    
        public void update(float dt) {
            lastFrameChanged += dt;
    
            if (lastFrameChanged > frameDuration) {
                lastFrameChanged = 0;
                r.run();
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 01:32

    He're is simplier solution, works fine in my app.

      public class MyActivity extends Acitivity {
    
        TextView myTextView;
        boolean someCondition=true;
    
         @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.my_activity);
    
                myTextView = (TextView) findViewById(R.id.refreshing_field);
    
                //starting our task which update textview every 1000 ms
                new RefreshTask().execute();
    
    
    
            }
    
        //class which updates our textview every second
    
        class RefreshTask extends AsyncTask {
    
                @Override
                protected void onProgressUpdate(Object... values) {
                    super.onProgressUpdate(values);
                    String text = String.valueOf(System.currentTimeMillis());
                    myTextView.setText(text);
    
                }
    
                @Override
                protected Object doInBackground(Object... params) {
                    while(someCondition) {
                        try {
                            //sleep for 1s in background...
                            Thread.sleep(1000);
                            //and update textview in ui thread
                            publishProgress();
                        } catch (InterruptedException e) {
                            e.printStackTrace(); 
    
                    };
                    return null;
                }
            }
        }
    
    0 讨论(0)
  • 2020-11-22 01:35

    for whom wants to do this in kotlin:

    val timer = fixedRateTimer(period = 1000L) {
                val currentTime: Date = Calendar.getInstance().time
                runOnUiThread {
                    tvFOO.text = currentTime.toString()
                }
            }
    

    for stopping the timer you can use this:

    timer.cancel()
    

    this function has many other options, give it a try

    0 讨论(0)
提交回复
热议问题