Home > Net >  Change int value in Android Studio
Change int value in Android Studio

Time:03-21

How do i change the "64" to a random number from 40 to 120 every 20 secondsin Android Studioenter image description here? enter image description here

CodePudding user response:

Here is a Simple Way to use forever loop which will generate random number between minimum and maximum after every given seconds.

    new Thread(() -> {
        while (true){
            final int min=40, max =120;
            final int second = 20;
            Random random = new Random();
            int rn = random.nextInt(max);
            if (rn<min) rn =min;
            int randomNumber = rn;
            new Handler(Looper.getMainLooper()).post(() -> {
                //run the code to show random number in UI...
                //tv.setText("" randomNumber);
                Log.d("xyz", "showNotification: "  randomNumber);

            });
            try {
                Thread.sleep(1000*second);//sleep for 20 second
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }).start();

CodePudding user response:

There is different way to do this type of task. One of the easy method is using timer and and calling it after a certain period of time. You can call handler from onResume(). This guy has make it a bit clear https://stackoverflow.com/a/40058010/10926972

Hope it works for you.

  • Related