Since i did eventually work it out, i might as well post the answer here for anyone who searches in the future:
final Handler handler = new Handler() { @Override public void handleMessage(Message msg) { Toast.makeText(getApplicationContext(), "Run!", Toast.LENGTH_SHORT).show(); } }; class ScheduledTaskWithHandeler implements Runnable { @Override public void run() { handler.sendEmptyMessage(0); } } final ScheduledExecutorService locationUdateScheduler = Executors.newScheduledThreadPool(1); scheduleHandle = locationUdateScheduler.scheduleAtFixedRate(new ScheduledTaskWithHandeler(), 1, 5, TimeUnit.SECONDS); The reason is: scheduled tasks are run in another thread, so you must used a handler (which lives on the main thread) to do the work. Any messages sent to the handler are processed the next time the main thread processes messages and events (near instant if the main thread is not busy). On Oct 2, 10:46 pm, aefaradien <h...@aefaradien.net> wrote: > Hi guys, > > I have spend over an hour trying to make this work now. I must be > missing something obvious, but no amount of searching is working. > > I am trying to schedule a task to run every few minutes, and i am > trying to do it using scheduleAtFixedRate(). > > I am testing this in a very basic hello world app to isolate it from > the rest of my project. I have a very simple app with one Activity > and in its onCreate() method i have the following code: > > @Override > public void onCreate(Bundle savedInstanceState) { > super.onCreate(savedInstanceState); > setContentView(R.layout.main); > > final Runnable task = new Runnable() { > public void run() { > Toast.makeText(getApplicationContext(), > "RUN!", > Toast.LENGTH_SHORT).show(); > } > }; > final ScheduledExecutorService locationUdateScheduler = > Executors.newScheduledThreadPool(1); > locationUdateScheduler.scheduleAtFixedRate( > task, 1, 10, TimeUnit.SECONDS); > } > > For some reason i can not work out, when i run this I never see the > toast message. Any help debugging this would be much appreciated. > > p.s. i have a vague hunch it might be because i have not added > something to the manifest file, but searching around the internet > reveals nothing of relevance. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Beginners" group. To post to this group, send email to android-beginners@googlegroups.com To unsubscribe from this group, send email to android-beginners-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~----------~----~----~----~------~----~------~--~---