StopSelf不会停止我的服务我的、StopSelf

2023-09-06 16:36:23 作者:孤僻浪子i

谷歌搜索的时候,我已经看到了这个问题发表了很多次,有些职位在这个论坛上,去年的几年。我从来没有见过任何人都得到一个明确的答案。

Googling around, I have seen this problem posted many times, some of the posts on this forum over that last few years. I have never seen anyone get a straight answer.

我试图做一个this.StopSelf运行2小时后自行停止()一个前台服务。我已经试过了,当它的约束,当它的绑定。在AVD和设备上。

I have a foreground service that tries to stop itself after running for 2 hours by doing a this.StopSelf(). I've tried it when it's bound and when it's unbound. On the AVD and on a device.

这根本不​​起作用。这是Android的一个错误? (运行2.3.3)。

It simply does not work. Is this a bug in Android? (running 2.3.3).

如何才能服务站本身?

推荐答案

如果您使用的是前台服务,你应该叫:

If you use a foreground service, you should call:

stopForeground(true);

就像你启动 startForeground

stopSelf 是指用于正常的服务。前台服务是特殊的...:)

stopSelf is meant to be used for normal services. a foreground service is special... :)

顺便说一句,如果您的应用程序应该在较旧的设备,应检查所提供的兼容性code here.

By the way, in case your app should work on older devices, you should check the compatibility code offered here.