Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Service keep alive

I have a small chat application. When it starts it binds to a service. This service has a tcp/ip connection with the server.

When the application is not in foreground my service creates a status notification. So far so good.

When the application is destroyed for example using task manager on "onDestroy" method I call unbind. Now the service is killed. So my question is how I make a service to stay alive even there is no clients bind with it.

Googe doc: " The service will remain running as long as the connection is established (whether or not the client retains a reference on the service's IBinder)."

Thanl you.

like image 455
Catalin Avatar asked Aug 18 '12 19:08

Catalin


People also ask

What is keep alive service on Android?

What Does Keepalive Mean? A keepalive is a signal sent from one device to another to maintain a connection between the two devices. This may be between a client and a server, but it could apply to any number of devices or technologies.

What is foreground service in Android?

Foreground services perform operations that are noticeable to the user. Foreground services show a status bar notification, so that users are actively aware that your app is performing a task in the foreground and is consuming system resources.


1 Answers

You can make your service a foreground service, which will display an icon in the notification bar so that the user can see your service is running. Otherwise, you cannot keep your service running. By the way, you should be aware that an everlasting service is an Android antipattern, the system should be able to kill off your app when memory is low, and apps haven't used it in a while. If you are doing something that legitimately needs to live "forever" you should do so with a foreground service.

like image 142
Kristopher Micinski Avatar answered Sep 30 '22 07:09

Kristopher Micinski