如何在Android中检测应用程序的退出?

问题描述:





比标题说的更精确:

我正在使用SIP应用程序 sticky开始启动Sip堆栈时的服务,即使应用程序在后台运行很长时间,甚至当Android开始在资源变低时清除东西时仍保持活动状态。

minApi等级为15 。



我的技术问题是:

- 当用户从开放应用程序关闭应用程序时,我能以某种方式检测刷卡列表?或者这是一个简单而简单的杀死我的应用程序的过程,并且在被杀之前没有可用的钩子来执行最后的操作吗?



我需要实现什么是:

- 服务始终保持活动状态,无论应用程序是在前台还是后台,无论它在那里停留多长时间。

- 应用程序必须按主要活动(拨号器)上的后退按钮时不会退出 - 这是产品所有者的要求 - 该部分已解决。

但是...... - 这里开始我的问题:

- 当用户杀死应用程序时(即从应用程序列表中滑动),sip堆栈将执行关闭并且用户通过sip不可用 - 即离线 。技术为我的应用程序,这意味着:关闭Sip /停止粘性服务。



采取什么方法?



感谢任何tipps,这让我发疯。

欢呼,Mike

Hi,

to be more precise, than the title says:
I am working on a SIP App which uses a "sticky" Service when starting to boot the Sip stack and to keep it alive even when the app is in background for a long time and even when Android starts purging things when resources get low.
minApi Level is 15.

My technical Problem is this:
- Can I somehow "detect" when the user closes the app from the open-apps-list with a swipe? Or is this a simple and plain "kill" of my app's process and there is no available hook to perform some last action before getting killed?

What I need to achieve is:
- Service stays active all the time, no matter whether the app is in foreground or background and no matter for how long it stays there.
- The App must not be "exited" when pressing the back button on the main activity (dialer) - this is by requirement of the product owners - that part is solved.
BUT... - and here starts my problem:
- When the user is "killing" the app (i.e. swipe from the app list), the sip stack shall perform a shutdown and the user is "not available" via sip - i.e. "offline". Technical for my app, this means: Shutdown Sip / Stop the sticky service.

What approach to take here?

Thanks for any tipps, this is driving me crazy.
cheers, Mike

onDestroy()和onStop()如果应用程序从最近的应用程序列表中关闭或被android系统杀死,则不会被解雇但是可能有这样的解决方法:



如何通过在Android中刷卡杀死应用程序时处理代码? - 堆栈溢出 [ ^ ]

android - 如何检测从最近列表中删除的应用程序 - Stack Overflow [ ^ ]
onDestroy() and onStop() wouldn't be fired if app is closed from recent apps list or killed by android system. But there may be a workaround like these:

How to handle code when app is killed by swiping in android? - Stack Overflow[^]
android - How to detect app removed from the recent list - Stack Overflow[^]


不确定如果你能得到你所要求的但是 Android开发者 [ ^ ]显示了你可以使用的各种回调。
Not sure if you can get what you are asking but Activity | Android Developers[^] shows the various callbacks you can use.


我不确定它是否可以工作你不能创建startForeground。只需确保在onStartCommand中START_STICKY即可。它仍然会终止服务,但它会在几秒钟后再次自动启动服务。希望我所说的可以帮助你。
I am not sure if it will work by can't you make the startForeground. Just make sure you START_STICKY in the onStartCommand. It will still kill the service but it should automatically start the service once again after a few seconds. Hopefully what I said helps you out.