[android-beginners] I can't make a scheduled task fire
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 -~--~~~~--~~--~--~---
[android-beginners] Re: I can't make a scheduled task fire
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 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 -~--~~~~--~~--~--~---
[android-beginners] Querying AlarmManager to determine if alarms are set
Hi there, does anyone know if its possible to query the AlarmManager to see if/what alarms are set? I can set an alarm using: AlarmManager am = (AlarmManager)getApplicationContext().getSystemService (Context.ALARM_SERVICE); Intent i = new Intent(getApplicationContext(), OnAlarmReceiver.class); PendingIntent pi=PendingIntent.getBroadcast(getApplicationContext(), 0, i, PendingIntent.FLAG_CANCEL_CURRENT); am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), Const.UPDATE_INTERVAL_MS, pi); And i can then cancel it using: am.cancel(pi); But i can not figure out how to determine if the alarm is still set (e.g. so an activity can inform the user that the scheduled task is already set). AlarmManager has not "isSet()" method. Can anyone shed any light on this? Is it even possible? many thanks in advance. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---