Android: "Auto refresh" after a certain time Android: "Auto refresh" after a certain time multithreading multithreading

Android: "Auto refresh" after a certain time


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.


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.

@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    doTheAutoRefresh();}


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