Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: "Auto refresh" after a certain time

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 :)

like image 456
João Silva Avatar asked Jun 14 '13 16:06

João Silva


3 Answers

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.

like image 105
Ken Wolf Avatar answered Sep 17 '22 15:09

Ken Wolf


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();
}
like image 34
TechnicalKalsa Avatar answered Sep 19 '22 15:09

TechnicalKalsa


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

like image 22
Hardik Gajera Avatar answered Sep 20 '22 15:09

Hardik Gajera