0

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.

Amr El Aswar
  • 3,395
  • 3
  • 23
  • 36
  • Word `auto refresh` is incomplete, what you are intending to achieve is understandable. – Murtaza Khursheed Hussain Oct 26 '15 at 16:30
  • I don't know what your app's requiremnets are, but it seems you should create a less resources-intense solution. – SuperFrog Oct 26 '15 at 16:32
  • @UdiIdan this is a seperate activity not the main one, the urlfind() method gets data from a website using jsoup and returns the string that I need to update on the TextView – Amr El Aswar Oct 26 '15 at 16:34
  • It doesn't really matter as you still using the resources of a mobile device. For the very least you should do the "auto-refresh" at a fixed reasonable interval and not as an infinte recursive function. – SuperFrog Oct 26 '15 at 16:36
  • @UdiIdan yes I understand, and how can that be done ? can i use a Thread.sleep() method for that or would that freeze the UI ? – Amr El Aswar Oct 26 '15 at 16:50
  • Read this, it might help: http://stackoverflow.com/questions/18605403/timertask-vs-thread-sleep-vs-handler-postdelayed-most-accurate-to-call-functio – SuperFrog Oct 26 '15 at 16:53
  • @UdiIdan thanks for the link, But I am already familliar with threading and handlers but this seemed like a simpler way to do it than creating a new thread and having to add a handler etc but what I would like to know is if it has any downsides. – Amr El Aswar Nov 01 '15 at 18:15

1 Answers1

0

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.

cprakashagr
  • 751
  • 11
  • 28
  • It turned out to be a teally bad idea to use AsynTask in that way it crashes your app at the end , I ended up using the Thread class with handlers instead, never crashes and more faster – Amr El Aswar Dec 04 '15 at 06:00
  • Using ScheduledExecutorService, must not crash your app. It is very flawless and accurate. http://stackoverflow.com/questions/18605403/timertask-vs-thread-sleep-vs-handler-postdelayed-most-accurate-to-call-functio?answertab=active#tab-top – cprakashagr Dec 04 '15 at 06:06