timertask

How to call a thread to run on specific time in java?

瘦欲@ 提交于 2019-12-04 10:41:54
问题 I want o make threads execute at specific exact times (for example at: 2012-07-11 13:12:24 and 2012-07-11 15:23:45) I checked ScheduledExecutorService , but it only supports executing after specific period from the first run and I don't have any fixed periods, instead I have times from database to execute tasks on. In a previous question for a different problem here, TimerTask was the solution, but obviuosly I can't make thread a TimerTask as Runnable and TimerTask both have the method run

Timer Task stops running after indefinite time in android

怎甘沉沦 提交于 2019-12-04 10:12:40
I am a newbie in android. I am developing an app in which a particular piece of code executes after every 5 seconds in background.To achieve this I am using a service with timer with a timer task in it. For sometime its working fine but after some indefinite my service is running but timer task stops automatically in android. Here is my code please help. Thanks in advance. public void onStart(Intent intent, int startid) { //this is the code for my onStart in service class int delay = 1000; // delay for 1 sec. final int period = 5000; // repeat 5 sec. timer = new Timer(); timer.schedule(new

Service crashes with nullpointerexception at onCreate

对着背影说爱祢 提交于 2019-12-04 06:22:25
问题 i wanted to check the actual time every 5 mins in a service and mute or unmute the phone depending on the time. earlier i tried to use a while(true) with thread.sleep(300000) at the end, but it crashed always with a ANR, so i tried to use a timer task but now it crashes right after the line AudioManager audioManager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE); with an NullPointerException package de.nathan.android.droidschool; import android.app.Service; import android

How to relaunch a TimerTask

╄→尐↘猪︶ㄣ 提交于 2019-12-04 05:32:47
问题 I have written a task to send a certain TCP message through a socket. I have a file with a bunch of messages and some timestamps, so I programmed the task as a TimerTask, and I scheduled it with a Timer with the first message timestamp. When it finishes, the task run method is over, but its associated thread remains, it's not cancelled. If I try to reschedule the task with a new Time, I'm getting an exception telling me that I cannot reschedulle a schedulled or cancelled task. I also tried

Java: Wait for TimerTask to complete before continuing execution

不想你离开。 提交于 2019-12-04 05:20:50
问题 I'm having a bit of an annoying problem. Right now, I have a snippet of code that starts a thread, sets a timer within that thread, and then exits that thread and continues with its life. My intent here was for the program to wait for the TimerTask to complete before continuing with code flow. However, obviously, setting up a new TimerTask doesn't pause execution to wait for the timer to run down. How do I set this up so that my code reaches the TimerTask, waits for the TimerTask to expire,

Android: Implication of using AsyncTask to make repeated Ajax Calls

∥☆過路亽.° 提交于 2019-12-04 04:50:20
问题 I need my Android app to periodically fetch data from a server using AJAX calls, and update the UI accordingly (just a bunch of TextView s that need to be updated with setText() ). Note that this involves 2 tasks: Making an AJAX call, and updating the UI once I receive a response - I use a simple AsyncTask for this. Doing the above repeatedly, at regular intervals. I haven't figured out an elegant way to achieve Point 2 above. Currently, I am simply executing the task itself from

How to implement a efficient timeout in java

我是研究僧i 提交于 2019-12-03 17:19:12
There are n object which perform some actions. After performing an action a timestamp will be updated. Now I want to implement a timeout-thread which verifies if a timestamp is older than for example 60 seconds. My first solution was to do that with a thread (while-loop + sleep) which is holding a list with all objects including the last timestamp. Now I have the problem that there is a worst-case scenario where the thread needs 59 seconds plus sleep time to decide for a timeout. I’m searching for a solution like a Timer where it is possible to update the delay time. Any ideas? I think using a

Getting metadata from SHOUTcast using IcyStreamMeta

雨燕双飞 提交于 2019-12-03 15:33:38
I am writing an app for Android that grabs meta data from SHOUTcast mp3 streams. I am using a pretty nifty class I found online that I slightly modified, but I am still having 2 problems. 1) I have to continuously ping the server to update the metadata using a TimerTask. I am not fond of this approach but it was all I could think of. 2) There is a metric tonne of garbage collection while my app is running. Removing the TimerTask got rid of the garbage collection issue so I am not sure if I am just doing it wrong or if this is normal. Here is the class I am using: public class IcyStreamMeta {

Android Asynctask vs Runnable vs timertask vs Service

空扰寡人 提交于 2019-12-03 07:58:13
问题 What are the differences between these methods (classes)? I want to run a app that runs every 5 seconds, clear the memory when it is finished and when the cpu is in standby mode, that you can run the app. So that the app is not bound to a wakelock. Regards, Shafqat 回答1: The difference between first three is just the amount of work that has been done for you. And a Service is a fundamental Android application component. AsyncTask as a convenience class for doing some work on a new thread and

When a Java TimerTask is scheduled in a Timer, is it already “executing”?

牧云@^-^@ 提交于 2019-12-03 06:51:47
I would like to clarify something about TimerTask. When you have the code below: timer.schedule(task, 60000); where the task is scheduled to run in the next 1 minute, is the task object already executing? because somewhere in my code I called task.cancel() but it seems that the call doesn't prevent task to be executed. I even logged the return value from the call and it returns false. I came about my question when I read the documentation for the cancel method: Cancels the TimerTask and removes it from the Timer's queue. Generally, it returns false if the call did not prevent a TimerTask from