Android - Keep Running Service

0

I have a system that uses Services to run in the background. It works fine while the app is open or closed, but in the list of recent apps. When I remove this list, I get the log I/ActivityManager: Killing 4419:com.cybermickey2077/u0a34 (adj 16): remove task or something similar, and the Service stops. How do I keep the Service running even with the application completely closed?

Code:

public class TimeCounter extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Contador count = new Contador();
        count.start();
        return START_STICKY;
    }

    class Contador extends Thread {
        public void run() {
            while (true) {
                this.verAlarmes();

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        public void verAlarmes() {
            Calendar calendar = Calendar.getInstance();
            SimpleDateFormat format = new SimpleDateFormat("HH:mm");
            String time = format.format(calendar.getTime());
            Log.e("verAlarmes()", "Agora: " + time);
        }
    }
}
    
asked by anonymous 15.07.2018 / 22:32

0 answers