0
How to run a permanent service in the background. For example I want my app to run this method over and over again :
public void addToCalendar(){
myDB = CustomApplication.getDatabaseHelper();
ColorDrawable blue = new ColorDrawable(Color.BLUE);
for(int i=1;i <= myDB.getLastId();i++){
String dt = myDB.getDates(i);
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("dd-MM-yyyy");
Date teste = null;
try {
teste = sdf.parse(dt);
} catch (ParseException e) {
e.printStackTrace();
}
caldroidFragment.setBackgroundDrawableForDate(blue,teste);
}
}
It helped me but as always running in the background should be expensive I have a doubt. Do you know how to perform an action when changing tab ? I’ve looked it up, but there’s a Tabhost who I don’t know what it is .
– Pedro Gouveia
I don’t understand how you are doing this. Why use an Asynctask when Timer already runs Timertask in another Thread?
– ramaral
@ramaral I advise you to try for a code that runs quite a lot, this way you thought. But think this, every 1 second Timertask will run the run(), which will happen if your task has not finished executing the run() commands that were previously called?
– Tiago Almeida
@Pedrogouveia If you are using tabs in your APP, I believe you have a "Pagevieweradapter", and how you want to perform an action when changing the tab, use Viewpager.onPageChangeListener in your Adapter. I’ll leave you a video link to see if it helps (tabs creation): https://www.youtube.com/watch?v=00LLd7qr9sA,
– Tiago Almeida
@Tiagoalmeida You’re right that’s why a Timer should only be used to perform short tasks. However Asynctask suffers from the same problem when using the method
execute()
. Both, Timer andAsyncTask.execute()
use only one thread, so the next task is only started when the previous one is finished. Your code would only solve this problem if you usedAsyncTask.executeOnExecutor()
, but would continue to be a "gambiarra".– ramaral
@ramaral, but if each call of the run(), you call a "new Asynctask(). execute, "for example, you’re creating another thread, not running the previous one. In other words, you will create several independent threads, and each one will perform its "service". But then you have to be careful not to run too many heavy threads, because it can slow the device down by running so many heavy threads in the background. It’s got to be fast play.
– Tiago Almeida
You’re mistaken, the Asynctask, when initiated by
execute()
, are all executed in the same Thread, see the note in documentation, or look at mine reply to Sequence of executions for Asynctask.– ramaral
@ramaral, I understood what you meant, and you are correct in your point of view. But, I I’m referring to the interruption of Asynctask, which can occur if you don’t have an Asynctask in the timer run() method, do you understand? (Which is what you asked me, why an Asynctask inside a Thread) With Asynctask in the run() method, this will ensure that the code, which is within Asynctask, runs from start to finish, without interruption. Regardless of how many Asynctasks are there within the run() method, they will all be running what they have to run.
– Tiago Almeida
I don’t know what you mean. The only reason I see to have the code like this is so that the same task is started, in the stipulated time, even if the previous one has not finished. If that’s the idea I’d replace Asynctask with an Executor and use a Handler instead of the Timer. Anyway it’s weird to want to run the same task before finishing the previous.
– ramaral