Permanent service in background

Asked

Viewed 79 times

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);
    }
}

1 answer

1


If you want to run in the background, you will need to use a Asynctask.

Within Asynctask you place this implementation.

And to run it from time to time, you can use a timer.

final Handler handler = new Handler(); Timer timer = new Timer(); TimerTask task = new TimerTask() {
@Override public void run() { handler.post(new Runnable() { public void run() { new MinhaTask().execute(); //Dentro da Task tu coloca seu código } }); } }; timer.schedule(task, 0, 1000); //1000ms

I hope I’ve helped!

  • 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 .

  • I don’t understand how you are doing this. Why use an Asynctask when Timer already runs Timertask in another Thread?

  • @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?

  • @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,

  • @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 and AsyncTask.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 used AsyncTask.executeOnExecutor(), but would continue to be a "gambiarra".

  • @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.

  • 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, 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.

  • 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.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.