This code can be used to automatically update your activity over time, as shown below.
You can use the header namespace first, as shown below.
import android.os.Handler;
After creating a new instance of Handler .
private final Handler handler = new Handler();
Then write one method for updating, as shown below, and call the OnCreate method area, here I used 5000 milliseconds or 5 seconds. You can change your desire.
private void doTheAutoRefresh() { handler.postDelayed(new Runnable() { @Override public void run() {
The final code for the put method is to automatically update as shown below.
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); doTheAutoRefresh(); }
source share