简体   繁体   English

应用程式无法关闭的Android服务

[英]Android service that doesn't close with application

I have been searching the net for hours. 我已经在网上搜索了几个小时。 I am trying to make an application that has a UI interface and a service running in the background for SIP phone communication, kind of like Skype. 我正在尝试制作一个具有UI界面和在后台运行的服务以进行SIP电话通信的应用程序,类似于Skype。

The UI starts and stops the service based on UI events, and the service stays logged in with a internet server in the background. UI将根据UI事件启动和停止服务,并且该服务保持在后台通过Internet服务器登录。 I have found many articles talking about running the service on a separate thread(done), using startService as opposed to binding the service(done) but whenever I use the task manager to kill the application as a user might, I get an error popup saying my application has crashed and the service no longer runs. 我发现很多文章都在谈论在单独的线程(完成)上运行服务,使用startService而不是绑定服务(完成),但是每当我使用任务管理器以用户身份杀死应用程序时,都会出现错误弹出窗口说我的应用程序崩溃了,该服务不再运行。

How do programs like Skype, Facebook, or email clients do this? Skype,Facebook或电子邮件客户端之类的程序如何做到这一点?

Do I have to run these as separate applications using implicit intents? 我是否必须使用隐式意图将它们作为单独的应用程序运行?

Is there some settings I have to set in the manifest file other than declaring the service and it's name? 除了声明服务及其名称之外,我还需要在清单文件中进行一些设置吗?

Better yet, is there a link to some page or source example using this kind of service? 更好的是,是否存在使用此类服务​​的某些页面或源示例的链接?

EDIT: Sorry, I guess I wasn't clear. 编辑:对不起,我想我不清楚。 The service is stopping, and I don't want it to. 服务正在停止,我不希望它停止。 I am trying to keep the service running in the background even after a user kills the application with the application manager. 我试图让服务在后台运行,即使在用户使用应用程序管理器终止应用程序后也是如此。

One of the more confusing things with Service is that it is not run in a separate thread by default. Service比较混乱的事情之一是,默认情况下,它不在单独的线程中运行。 Calling startService() as opposed to bindService() makes no difference in this regard. 在这方面,调用startService()而不是bindService()没有区别。 However, the binder mechanism will cause the Service exposed methods to be called in arbitrary thread context. 但是,绑定器机制将导致在任意线程上下文中调用公开Service方法。 In both cases, your Service is part of your application's process. 在这两种情况下,您的Service都是应用程序流程的一部分。 If you kill it via the task manager or it crashes then the Service dies as well. 如果您通过任务管理器将其杀死或崩溃,则该Service也会死亡。

When you kill the app via the task manager and it pops up the message about the app dying, you have something misbehaving. 当您通过任务管理器中止该应用程序时,它弹出有关该应用程序即将死亡的消息时,您的行为不正常。 Check logcat and it will point you at exactly where the crash happened. 检查logcat,它将指向您确切的崩溃发生位置。

If you need to have your Service running regardless of the UI, then don't stop the Service when your UI exits. 如果您需要在不考虑用户界面的情况下都运行Service ,则不要在用户界面退出时停止Service You will still call startService() when your UI starts to re-connect (and possibly re-start it), but don't stop it unless you really want it stopped. 当用户界面开始重新连接(并可能重新启动startService()时,您仍然会调用startService() ,但是除非您确实希望它停止,否则不要停止它。 Starts do not "stack". 启动时不要“堆叠”。 If something calls start 5x it doesn't take 5 stops to terminate the Service , only 1. 如果某次呼叫以5倍开始,则无需5停就可以终止Service ,而只需1次即可。

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

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