Leave app in the background when closing Android application

1

In my main activity, I'm doing that every time there is internet send the sqlite data to the server of mysql clear only those that have not synchronized all that I have solved but the problem is that I want my application to close it already be removing it with the adm. of tasks this continues working and verifying if there is internet.

Thread timerTread = new Thread(){
        public void run(){
            try{
                sleep(2000);
            }
            catch (InterruptedException e){
                e.printStackTrace();
            } finally {
                //<<<<<<<<<<<<<<<<<<<<<<<<<< Aqui el envio de mi información
            }
        }
    };
    timerTread.start();

I do not know if I'm doing the correct operation for sending information, I want it to be sent, for example, every 2000ms, is this the correct way? How then do I prevent my app from closing when performing this type of operations?

    
asked by DoubleM 20.12.2016 в 07:45
source

1 answer

1

To make an application that runs in the background you have to inherit from the Service class and call the startService () and stopService () methods. You have more information on the Android page

Afterwards, if you want the application to launch automatically every X time you have to use the Android object AlarmManager , which allows defining the launch of Intents in a certain moment of time. The object is obtained like this:

(AlarmManager) getSystemService(ALARM_SERVICE);
    
answered by 20.12.2016 в 08:50