Different way of running a loop on AsyncTask java Android

So I needed a while loop that would auto-refresh the text on the layout and I finally found a way like this (I only put the important part so you get the idea) :

    public void restart() {
    GetRate asyncRate = new GetRate();
    asyncRate.execute();


}


private class GetRate extends AsyncTask<Void, Integer, String> {

    @Override
    protected String doInBackground(Void... params) {

        try {
            String p = urlfind();
            return p;
        }

        catch (IOException e) {
            return "0";
        }
    }

    @Override
    protected void onPostExecute(String p) {
        price_text.setText(p);
        restart();

    }

now my question is, is this a good way of getting this done or could this cause problems ? the app seems to be working fine and I am not getting any errors for the moment, if there is a better or simpler way to do this please give an answer. thanks

Edit : this turned out to be a really bad idea even when I added sleep intervals the app would work for a 15min then crash so I dont advise anyone to use this.

Answers


Instead of calling the thread manually, AlarmManager or ScheduledExecutorService could be used to fire the task at certain intervals or at certain time.

AlarmManager is generally used for large intervals and the later one for short intervals.

Using these classes, you can fire your task and get your UI updated.


Need Your Help

Loop through each row of a range in Excel

excel vba excel-vba loops excel-2003

This is one of those things that I'm sure there's a built-in function for (and I may well have been told it in the past), but I'm scratching my head to remember it.

jQuery Autocomplete change firing only after submitting

javascript jquery html forms autocomplete

I've needed to do some things on Change event of jQuery Autocomplete. It's working as I expected (doing these things after changing and losing focus).