Android的服务永葆 [英] Android Service keep alive

查看:116
本文介绍了Android的服务永葆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小的聊天应用程序。当它启动时,它结合到服务。此服务与服务器的TCP / IP连接。

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.

当应用程序使用任务管理器的onDestroy我称之为解除绑定破坏的例子。现在,该服务被杀害。 所以我的问题是我如何做一个服务,为了生存,甚至没有客户端绑定吧。

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文档:服务将仍然只要建立连接(客户端是否保留在服务的​​的IBinder基准)运行

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你。

推荐答案

您可以让您的服务前台服务,这将在通知栏显示一个图标,使用户可以看到您的服务正在运行。否则,你的不能的保持你的服务运行。顺便说一句,你应该知道,永远的服务是一个Android反模式,该系统的的能杀了你的应用程序,当内存不足,和应用程序没有用它在一段时间。如果你正在做的事情是合法需要生活永远,你应该有一个前台服务这样做。

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.

这篇关于Android的服务永葆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆