Android: Auto-update after a certain time

I have a search, how can I do an “Automatic update” or a launch method for my program, I saw several messages about handlers and threads ... I think that what I am looking for from a stream, but I can not get the program to work ... Let me show you the 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 piece of code should get the number from the user, and then do getUrlText (); every x minutes, where x is the number that the user enters ... My problem should be at startup, but I can’t understand what it is ... Thanks in advance for the help :)

+6
source share
3 answers

Here is one way to do it.

Announce them in your activity:

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

Then set the onClick value:

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

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

I'm not sure what you are doing with mins and so on, but put all your logic that should be executed periodically in timedTask Runnable . Hope this makes sense.

+9
source

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() { // Write code for your refresh logic doTheAutoRefresh(); } }, 5000); } 

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(); } 
+6
source

1. create Handler

 Handler handler = new Handler(); 

2. Create a Runnable Object

 public final Runnable runnable = new Runnable() { @Override public void run() { // your code while refreshing activity } }; 

3. Method of calling the handler object

 handler.postDelayed(runnable, 3000); 

// 3000 - time in milliseconds

// put this method in onCreate ()

// onResume () or Where the update occurs

+2
source

Source: https://habr.com/ru/post/947317/


All Articles