timertask

What should Timertask.scheduleAtFixedRate do if the clock changes?

我怕爱的太早我们不能终老 提交于 2019-12-17 12:36:15
问题 We want to run a task every 1000 seconds (say). So we have timer.scheduleAtFixedRate(task, delay, interval); Mostly, this works fine. However, this is an embedded system and the user can change the real time clock. If they set it to a time in the past after we set up the timer, it seems the timer doesn't execute until the original real-time date/time. So if they set it back 3 days, the timer doesn't execute for 3 days :( Is this permissible behaviour, or a defect in the Java library? The

Start Android Service after every 5 minutes

一笑奈何 提交于 2019-12-17 10:34:39
问题 I was searching over the internet for last 2 days but I couldn't find any tutorial helpful. I have created a service and I am sending a notification in status bar when the service starts. I want that service to stop after showing the notification and start it again after 5 minutes. Please let me know if it is possible and provide me some helpful tutorials if you have any. I heard of TimerTask and AlarmManager and I tried to use them as well but I wasn't able to get the desired result. EDIT: I

Android - loop part of the code every 5 seconds

℡╲_俬逩灬. 提交于 2019-12-17 06:45:50
问题 I would like to start repeating two lines of code every 5 seconds when I press the button START and end it, when I press the button STOP. I was trynig with a TimerTask and Handles, but couldn't figure it out how. public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //final int i; final TextView textView = (TextView) findViewById(R.id.textView); final Button START

TimerTask vs Thread.sleep vs Handler postDelayed - most accurate to call function every N milliseconds?

你离开我真会死。 提交于 2019-12-17 06:29:50
问题 What is the most accurate way to call a function every N milliseconds? Thread with Thread.sleep TimerTask Handler with postDelayed I modified this example using Thread.sleep and it's not very accurate. I'm developing a music app that will play sounds at a given BPM. I understand it's impossible to create an entirely accurate metronome and I don't need to - just looking to find the best way to do this. Thanks 回答1: There are some disadvantages of using Timer It creates only single thread to

How to run certain task every day at a particular time using ScheduledExecutorService?

自闭症网瘾萝莉.ら 提交于 2019-12-17 02:34:10
问题 I am trying to run a certain task everyday at 5 AM in the morning. So I decided to use ScheduledExecutorService for this but so far I have seen examples which shows how to run task every few minutes. And I am not able to find any example which shows how to run a task every day at a particular time (5 AM) in the morning and also considering the fact of daylight saving time as well - Below is my code which will run every 15 minutes - public class ScheduledTaskExample { private final

Timer and TimerTask - how to reschedule Timer from within TimerTask run

倾然丶 夕夏残阳落幕 提交于 2019-12-14 03:53:42
问题 basically what I want to do is to make a Timer that runs a specific TimerTask after x seconds, but then the TimerTask can reschedule the Timer to do the task after y seconds. Example is below, it gives me an error "Exception in thread "Timer-0" java.lang.IllegalStateException: Task already scheduled or cancelled" on line where I try to schedule this task in TimerTask run. import java.util.Timer; import java.util.TimerTask; public class JavaReminder { public JavaReminder(int seconds) { Timer

Thread and mediaplayer problems, Timer with sound

巧了我就是萌 提交于 2019-12-13 06:44:19
问题 I'm developing an Android application that is based on a timer that every second must play a sound that you selected before. Here is the problem: No objects can run Media Player in a Thread or a Runnable I can not call methods that reproduce the sound I can not run it on a timer task Can you think of anything? Thanks thread: public void run(){ while(!detenido) //Bolean for stop the thread { try { Servicio servicios = new Servicio(); switch(segundosesperar){ //Int to select the ms to slep case

Issue with TimerTask

蓝咒 提交于 2019-12-13 06:38:20
问题 Just have a look on this block of code: public Reminder() { a[0]=1000; a[1]=3000; a[2]=1000; a[3]=5000; timer = new Timer(); timer.schedule(new RemindTask(),0, a[i]); } ////////////////////// class RemindTask extends TimerTask { public void run() { point =point +arr[i].length(); doc.setCharacterAttributes(0,point+1, textpane.getStyle("Red"), true); i++; } } I want delay to be changed after each task,so the timings are stored in an array. When i++ is preformed(pointer to array),the timings are

Spring @Autowired annotation doesn't work in Java TimerTask

半世苍凉 提交于 2019-12-12 10:24:33
问题 How to change my code to make it work? public class GetDataFromTheWarehouse implements ServletContextListener { @Autowired ScheduledTask scheduledTask; private ScheduledExecutorService scheduler = null; public GetDataFromTheWarehouse() { } public void contextDestroyed(ServletContextEvent arg0) { try { System.out.println("Scheduler Shutting down successfully " + new Date()); scheduler.shutdown(); } catch (Exception ex) { } } public void contextInitialized(ServletContextEvent arg0) { if (

Android: Cancel Volley request during execution due to slow connection

邮差的信 提交于 2019-12-12 10:16:52
问题 I'm using Volley in Android in order to make requests including fetching relatively large amount of data. I want to make timer of 5 seconds and if after it the request not returned - it probably means that there is slow connection and therefore I want to cancel the request. So far what I did: Timer timer = new Timer(); VioozerVolleyRequestFactory mFactory = new VioozerVolleyRequestFactory(this); RequestQueue mQueue = VioozerVolleySingleton.getInstance(this).getRequestQueue(); timer.schedule