Lama
Lama

Reputation: 2966

Android Runnable won't repeat

I wan't to refresh a list of website every X Minutes.

After reading this thread: nested postDelayed / Runnable / Handler Android

I decided to use a Runnable + Handler instead of a TimerTask. Unfortunately my Runnable won't run a second time. It will refresh the data once, then nothing happens.

my onCreate() Method looks like this:

...    
fooWebsitesListView = (ListView) findViewById(R.id.fooWebsitesListView);

fooWebsitesAdapter = new ArrayAdapter<fooWebsite>(
    getApplicationContext(), R.layout.list_black_text,
    R.id.list_content, websiteList);

    fooWebsitesListView.setAdapter(fooWebsitesAdapter);
    final fooWebsitesActivity fooWebsitesRefreshActivity = new fooWebsitesActivity();

    mHandler = new Handler();

    refreshfooWebsitesRunnable = new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            Log.d("scheduled task", "---Scheduled Task: Refresh fooWebsites");
            try {
                fooWebsitesRefreshActivity.execute();
                //mHandler.postDelayed(refreshfooWebsitesRunnable,3000);
                mHandler.postDelayed(this, 3000);
            } catch (Exception e) {
                // Toast.makeText(getApplicationContext(),
                // "Unable to receive Data", Toast.LENGTH_SHORT)
                // .show();
            }
        }
    };
    startRepeatingTask();
}

void startRepeatingTask() {
    refreshfooWebsitesRunnable.run();
}

LOG:

12-14 14:56:47.624: D/scheduled task(7995): ---Scheduled Task: Refresh fooWebsites

And there has been no chance since yet. The UI Thread is working fine, the app won't freeze or anything like that. Why won't my runnable loop?

Upvotes: 1

Views: 460

Answers (1)

Dmide
Dmide

Reputation: 6462

Try to call mHandler.post() instead of refreshfooWebsitesRunnable.run() and call a mHandler.removeCallbacks() in the beginning of your run() method.

Upvotes: 1

Related Questions