1

I have an onCreate method that runs the code below. In a nutshell the code retrieves data from the server and shows it on the screen for a messaging program. It only does it once, but I would like it to run the AsyncTask every 3 seconds (to try to simulate a chat). I'm pretty sure this is not the way to go about having a chat system but, I just need something that works for now (as a proof of concept) and I'll focus on the correct way of implementing it later.

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chat_box);// sd

        final Functions function = new Functions();
        final SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(getBaseContext());
        whatroom = prefs.getString("chat", "null");

        new AsyncTask<String, Void, String>() {
            @Override
            protected String doInBackground(String... args) {
                return function.getInbox(args[0]);
            }

            @Override
            protected void onPostExecute(String result) {
                TextView inbox = (TextView) findViewById(R.id.inbox);
                ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar1);
                progressBar.setVisibility(View.GONE);
                inbox.setText(result);
            }

        }.execute(whatroom);
    }

I've tried putting a simple while statement around the asynctask but, it just force closes.

EGHDK
  • 17,818
  • 45
  • 129
  • 204
  • 1
    We usually don't use AsyncTask for periodical task, check out my answer [here](http://stackoverflow.com/questions/8098806/where-do-i-create-and-use-scheduledthreadpoolexecutor-timertask-or-handler/8102488#8102488) to see if it helps. – yorkw Apr 25 '12 at 23:22
  • Yorkw, your answer in that question was perfect. It worked for me and did everything I needed it to do. – EGHDK Apr 26 '12 at 03:19

2 Answers2

2

You cannot reuse an AsyncTask instance. You would need to create fresh instances each pass of your loop.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Should I bother trying to create a fresh instance, or should I go another route (like loaders mentioned by @Steve)? – EGHDK Apr 25 '12 at 23:22
  • @EGHDK: This does not feel like a `Loader` scenario to me. You might use `Timer` and `TimerTask`, plus `post()` on your `TextView` to get those updates to occur on the main application thread instead of the task's thread. – CommonsWare Apr 25 '12 at 23:27
  • So if anyone finds this in the future. CommonsWare gave the answer to the question but, yorkw gave me the solution. Thanks all for your time. I appreciate it. – EGHDK Apr 27 '12 at 04:24
0

Without additional information, it's difficult to give you a specific answer. However look into abstracting everything using a Loader, using a Service, etc

Regarding Loaders:

  • They are available to every Activity and Fragment.
  • They provide asynchronous loading of data.
  • They monitor the source of their data and deliver new results when the content changes.
  • They automatically reconnect to the last loader's cursor when being recreated after a configuration change. Thus, they don't need to re-query their data.
Steve
  • 53,375
  • 33
  • 96
  • 141
  • The asyncTask simply runs the function `getInbox()`, which gets information from the web using a php script. Would a loader be the right thing to use for this? – EGHDK Apr 25 '12 at 23:10