Application is closed after a while, even with service

Asked

Viewed 180 times

0

Hello I have a service that runs in the background, this service should not be finished by the Android system, but it happens after a run time.

public class VerificaCorridas extends Service {

private Timer timerAskServer;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    verifyServer();
    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
    super.onDestroy();
    timerAskServer.cancel();
}

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


@Override
public IBinder onBind(Intent intent) {
    return null;
}

public void verifyServer() {
    timerAskServer = new Timer();
    TimerTask askServer = new TimerTask() {
        public void run() {
            // Aqui é feito uma consulta ao servidor para ver se tem algo novo
        };

    };
    timerAskServer.schedule(askServer, 0, 10000); //repete a cada 10 Seg
}

}

  • 1

    Without knowing the service code becomes difficult.

  • I will try to reduce the code to post, but can it happen? A service it is finalized by the system?

  • Yes, it depends on how it is declared.

  • Ready posted the code

  • For this specific case and if you do not want to use the Google cloud messaging (GCM) use a Alarmmanager with a Broadcastreceiver. Behold here an example.

  • @Mateuscarvalho este APP que você está elaborando é para Entregas Rápido?

  • No @Andrébaill

  • @ramaral I use parse.com, it is already included the service of GCM, however there is a delay of time sometimes in the delivery of the message... That can’t happen, you can tell if it’s normal?

  • I don’t know, I’ve never used the Parse.

Show 4 more comments

2 answers

0

Speaking very generically, any Android service can be stopped at any time, in particular if the phone has little memory. That’s how the platform works.

An alternative to ensure that it comes back after an interruption is to restart it periodically with an Alarm.

Another alternative, quite intrusive, is to start the service with startForeground. In this mode a notification is permanently in the notification area (it cannot be "dismissed" by the user) which "holds" the service in memory.

Services that spend resources and run all the time should use this solution, according to the Android UI Guidelines. For example: a music app, which is using audio all the time and should not be stopped.

That being said, a service with permanent notification can STILL be stopped in case of absolute lack of memory. So Alarm is still required if you want to ensure that it will run 24x7.

0

As an Android developer, it’s important to keep in mind that you have no control over the lifecycle of an application. At any time your device may run out of resources (run out of memory for example) and the system will terminate the application to give keep the OS running properly.

This behavior is valid for any part of your application. This way, you need to use a AlarmManager (http://developer.android.com/reference/android/app/AlarmManager.html) to periodically fire their service and make the check. It is not legal to keep a service running forever, not to be intrusive and consume unnecessary user resources.

Browser other questions tagged

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