Permanent service in the background

0

How to run a permanent service in the background. For example I want my app to always run this method:

           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);
    }
}
    
asked by anonymous 12.07.2017 / 23:05

1 answer

1

If you want to run in the background, you will need to use 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 have helped!

    
12.07.2017 / 23:29