-1

after some time of programming I decided to download the Android SDK and see if I should start focusing on mobile development to start making money.

At the moment I am just trying to get used to how the android works and up until now I have a basic understanding of the events and the way the xml files interact with the .java files.

To learn I'm just trying to do tests of basic stuff and now I have something like this :

  TextView text = (TextView)findViewById(R.id.lol);
        number mnumber = new number();
        mnumber.change_in_number();
        text.setText(mnumber.get_in_number() + "");

Let me explain; number is a class I make which has an integer varibale inside, a function to get the value (get_in_number) and a function to change the integer variable to a random value (change_in_number). All of this functions work es intended for they are very simple but when I run the code this only happens once (as expected).

My question now is... Exactly how would I make this code repeat itself every X amount of seconds? You know, to have the value changed multiple times without the need of any event while the application is running.

I know this question is really simple and probably also easy to answer but right now I really need help with getting started. Thanks in advance for your help.

1 Answers1

0

This might be a lot over your head but you need to create a separate thread with a while loop to periodically update your TextView. I didn't compile and run this but it should be pretty close to exactly what you want:

public class YourActivity extends Activity
    {
    private UpdaterThread updaterThread;
    private Runnable changeNumberRunnable;

    @Override
    protected void onCreate(Bundle savedInstanceState)
        {
        super.onCreate(savedInstanceState);

        changeNumberRunnable=new Runnable()
            {
            @Override
            public void run()
                {
                YourActivity.this.updateNumber();
                }
            };
        }

    @Override
    protected void onResume()
        {
        super.onResume();

        updaterThread=new UpdaterThread();
        updaterThread.start();
        }

    @Override
    protected void onPause()
        {
        super.onPause();

        updaterThread.kill();
        }

    private void updateNumber()
        {
        TextView text = (TextView)findViewById(R.id.lol);
        number mnumber = new number();
        mnumber.change_in_number();
        text.setText(mnumber.get_in_number() + "");
        }

    private class UpdaterThread extends Thread
        {
        private boolean running;

        public void kill()
            {
            running=false;
            }

        @Override
        public void run()
            {
            running=true;

            while(running)
                {
                //you can't change a view from a separate thread, so call the update on the main UI thread
                YourActivity.this.runOnUiThread(changeNumberRunnable);

                //sleep for 5 seconds, if we're interrupted then exit the loop
                try { Thread.sleep(5000); }
                catch(InterruptedException e) { running=false; }
                }

            }
        }
    }
Brian DeWolff
  • 326
  • 1
  • 5