timertask

service automatically stops after several minutes

六月ゝ 毕业季﹏ 提交于 2019-11-30 16:00:45
问题 I'm creating a service which should work when the activity is in background as well as when the whole application is destroyed. I'm calling location coordinates in the service at every 1 minute interval. However, when I try to do so, the service shuts down automatically just after 12-15 minutes. I want the service to work endlessly until and unless it is destroyed by the completion of activity on user interaction. My service class is as follows: public class SensorService extends Service {

Java: Scheduling a task in random intervals

雨燕双飞 提交于 2019-11-30 15:23:06
问题 I am quite new to Java and I'm trying to generate a task that will run every 5 to 10 seconds, so at any interval in the area between 5 to 10, including 10. I tried several things but nothing is working so far. My latest effort is below: timer= new Timer(); Random generator = new Random(); int interval; //The task will run after 10 seconds for the first time: timer.schedule(task, 10000); //Wait for the first execution of the task to finish: try { sleep(10000); } catch(InterruptedException ex)

Java: Scheduling a task in random intervals

蹲街弑〆低调 提交于 2019-11-30 14:26:11
I am quite new to Java and I'm trying to generate a task that will run every 5 to 10 seconds, so at any interval in the area between 5 to 10, including 10. I tried several things but nothing is working so far. My latest effort is below: timer= new Timer(); Random generator = new Random(); int interval; //The task will run after 10 seconds for the first time: timer.schedule(task, 10000); //Wait for the first execution of the task to finish: try { sleep(10000); } catch(InterruptedException ex) { ex.printStackTrace(); } //Afterwards, run it every 5 to 10 seconds, until a condition becomes true:

Java - Timer.cancel() v/s TimerTask.cancel()

我的未来我决定 提交于 2019-11-30 01:55:02
问题 In my Android application, I run a timer and cancel it on some other event: class MyTimerTask extends TimerTask { override boolean cancel() { ... } override void run() { ... } } ... Timer t = new Timer(); t.schedule(new MyTimerTask(),...) ... t.cancel(); I was expecting t.cancel() to automatically invoke MyTimerTask 's cancel() method. But that method is never invoked. I am wondering what exactly is the different between these two methods and why the second method does not get called

app crashing with “Called From Wrong Thread Exception”

爱⌒轻易说出口 提交于 2019-11-29 06:12:38
I added this part of the code in my onCreate() method and it crashes my app. need help. LOGCAT: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. CODE: final TextView timerDisplayPanel = (TextView) findViewById(R.id.textView2); Timer t = new Timer(); t.schedule(new TimerTask(){ public void run(){ timerInt++; Log.d("timer", "timer"); timerDisplayPanel.setText("Time ="+ timerInt +"Sec"); } },10, 1000); Only the UI thread that created a view hierarchy can touch its views. You are trying to change the text of the UI

Attempt to invoke virtual method

可紊 提交于 2019-11-29 01:49:10
I am trying to make a simple timer using the Timer and TimerTask classes. I keep getting the following error: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference Here is my code: package com.austinheitmann.stopwatch; import android.os.CountDownTimer; import android.os.SystemClock; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget

how to auto change image in java swing?

安稳与你 提交于 2019-11-28 14:35:00
Hi i am creating a java desktop application where i want to show image and i want that all image should change in every 5 sec automatically i do not know how to do this here is my code public class ImageGallery extends JFrame { private ImageIcon myImage1 = new ImageIcon ("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\yellow.png"); private ImageIcon myImage2 = new ImageIcon ("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\d.jpg"); private ImageIcon myImage3 = new ImageIcon ("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\e.jpg"); private ImageIcon myImage4 = new ImageIcon ("E:\\SOFTWARE\\TrainPIS\\res\\drawable\f.jpg

Schedule a TimerTask at specific time once per day in a service in android

我的梦境 提交于 2019-11-28 13:11:06
I have a TimerTask object timerTask . My code is running but when service gets started initially, my timer task runs instantly before the specified time i.e. 3:30 PM, though I want it to run on 3:30 PM once per day only. Calendar cal = Calendar.getInstance(); cal.set(Calendar.AM_PM, Calendar.PM); cal.set(Calendar.HOUR, 3); cal.set(Calendar.MINUTE, 30); cal.set(Calendar.SECOND, 0); Date date = cal.getTime(); timer.schedule(timerTask, date, 1000*60*60*24); // once per day As other community users suggested, don't use Timertask, android has a AlarmManager that you can use. HOW? Register the alarm

Timer Task VS Alarm Manager usage in Android Service

我只是一个虾纸丫 提交于 2019-11-28 12:20:46
I need to fetch news/event updates from the server at regular intervals like for every 20mins in my Android App. AFAIK Intent Service and Broadcast Receiver combination will be better than using Service As I am not going to communicate with the running Service. In order to fetch events at regular intervals I know 2 options 1) Using Timer Task ScheduleAtFixedRate, I am going to start IntentService, which will fetch events once & broadcast if any updates and destroy itself. After Given Interval, IntentService will be again fired by TimerTask 2) Simply starting Intent Service at the start of app

Timer and TimerTask in Android

筅森魡賤 提交于 2019-11-28 11:56:13
I need a timer for my program. I have written it and it works fine on PC in emulalator program (Android 1.5/2.2). But it doesn't work on the real device (Android 1.5). What am I doing wrong? TimerTask task = new TimerTask() { public void run() { if (condition) { myFunc(); } else { this.cancel(); } } }; Timer timer = new Timer(); timer.schedule(task, 500, 85); You need to cancel() timer not the timer task. 来源: https://stackoverflow.com/questions/6477608/timer-and-timertask-in-android