停止前台服务
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
如果您希望前台服务停止在前台运行,可以选择以下两种方法。您可以停止服务,也可以让服务继续运行,但将其从前台移除。
您可以按照停止任何服务的方式停止前台服务。服务可以调用自己的 stopSelf()
方法,也可以通过调用 stopService()
来停止。如果您在前台运行时停止服务,则该服务的通知会被移除。
如需从前台移除服务,请从服务内部调用 stopForeground(int)
。此方法接受一个布尔值,用于指示是否也移除状态栏通知。该服务会继续运行,但不再是前台服务。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-21。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-21。"],[],[],null,["# Stop a foreground service\n\nIf you want a foreground service to stop running in the foreground, you have\ntwo options. You can stop the service, or you can leave the service\nrunning but remove it from the foreground.\n\nYou can stop a foreground service\n[the same way you would stop any service](/develop/background-work/services#Stopping). The service can\ncall its own [`stopSelf()`](/reference/android/app/Service#stopSelf()) method, or another component can stop it\nby calling [`stopService()`](/reference/android/content/Context#stopService(android.content.Intent)). If you stop the service while it runs\nin the foreground, its notification is removed.\n\nTo remove a service from the foreground, call\n[`stopForeground(int)`](/reference/android/app/Service#stopForeground(int))\nfrom inside the service. This method takes a boolean, which indicates whether to\nremove the status bar notification as well. The service continues to run, but\nit is no longer a foreground service."]]