Android: "Auto refresh" after a certain time
Asked Answered
R

3

8

i have search how i can do an "Auto refresh" or a runnable method for my program, i saw some posts about handlers and threads... I think that what im search from is a thread but i cant get the program to work... Let me show you some code:

refresh.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            getUrlText();
            if (time.getText().toString().equals("")
                    || time.getText().toString().equals("0")) {
                mins = 0;
            } else {

                mins = Integer.parseInt(time.getText().toString());
                setTimer(mins);
                t.start();


            }

        }

        private void setTimer(int mins) {
            miliSecTime = mins * 60 * 1000;

        }
    });

    t= new Thread() {
        @Override
        public void run() {
            long start = System.currentTimeMillis();

            while (true) {
                long time = System.currentTimeMillis() - start;
                int seconds = (int) (time / 1000);
                if (seconds > miliSecTime) {
                    getUrlText();
                    start = System.currentTimeMillis();

                }
            }
        }
    };
}

So, this part of code should get a number from the user and then execute getUrlText(); every x minutes, where x is the number that user input... My problem should be in the run but i can't figure out what is... Thank you in advance for the help :)

Rihana answered 14/6, 2013 at 16:50 Comment(1)
For something like that, you'd be better off using developer.android.com/reference/android/app/AlarmManager.html.Particia
K
10

Here's one way to do it.

Declare these in your Activity:

Handler handler = new Handler();
Runnable timedTask = new Runnable(){

    @Override
    public void run() {
        getUrlText();
        handler.postDelayed(timedTask, 1000);
    }};

Then set your onClick:

button.setOnClickListener(new OnClickListener() {

    @Override
        public void onClick(View v) {
            getUrlText();
            handler.post(timedTask);
        }
    });

This will execute timedTask every 1000 milliseconds. Increase this number as necessary.

I'm not sure what you're doing with mins and so on but put all your logic that needs to be executed periodically into the timedTask Runnable. Hope that makes sense.

Kaffiyeh answered 14/6, 2013 at 17:8 Comment(0)
W
6

You can use this code for implemented of Auto-Refresh of your activity for a certain times as below.

First you can use namespace of header as below.

import android.os.Handler;

After create new instance of Handler.

    private final Handler handler = new Handler();

Then write a one method for refresh as below and call in OnCreate method scope, here i have used 5000 milliseconds or 5 seconds. You can change as your wish.

private void doTheAutoRefresh() {
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
             // Write code for your refresh logic
            doTheAutoRefresh();
        }
    }, 5000);
}

Final code for put method of auto refresh as below.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    doTheAutoRefresh();
}
Wonderment answered 31/7, 2016 at 16:14 Comment(0)
H
2

1. create Handler

Handler handler = new Handler();

2. Create Runnable object

public final Runnable runnable = new Runnable() {

    @Override
    public void run() {
        // your code while refreshing activity
    }
};

3. Call Method of Handler Object

handler.postDelayed(runnable, 3000);

//3000 is time in milliseconds

// put this method in onCreate()

// onResume() or Where refreshing take place

Hwahwan answered 11/3, 2015 at 4:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.