简体   繁体   English

关机时不会销毁Android服务

[英]Android Service not destroyed on shutdown

I'm running an Android Service that is started after boot completed. 我正在运行启动完成后启动的Android服务。 However, when I shutdown my phone, the Service (and all to it connected Threads) don't seem to be destroyed. 但是,当我关闭手机时,服务(以及所有连接的线程)似乎都没有被破坏。

I have a Socket connection running in the background and it is not disconnected on shutdown. 我在后台运行Socket连接,并且在关机时没有断开连接。 Moreover, not even service.onDestroy() is called. 而且,甚至不调用service.onDestroy()。

Is that normal or might it be due to another problem of mine? 这是正常的还是可能是由于我的另一个问题?

您可以使用可以访问其他移动组件状态的Reciever,例如互联网连接状态,GPS状态,一旦移动启动,它将启动您的服务以获得简单示例

check the flag which you set as a return from onStartCommand : 检查从onStartCommand返回的标志:

START_STICKY START_STICKY

the system will try to re-create your service after it is killed 系统将在它被杀死后尝试重新创建您的服务

START_NOT_STICKY START_NOT_STICKY

the system will not try to re-create your service after it is killed 系统在杀死后不会尝试重新创建服务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM