使用前景服务的Context.startForegroundService(Intent)而不是Context.startService(Intent)有什么好处? [英] Are there any benefits to using Context.startForegroundService(Intent) instead of Context.startService(Intent) for foreground services?

查看:910
本文介绍了使用前景服务的Context.startForegroundService(Intent)而不是Context.startService(Intent)有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了文档 Context.startForegroundService()隐式承诺启动的服务将调用startForeground().但是,由于Android O即将对后台和前台服务进行更改,因此与使用较旧的startService()方法相比,它还有其他性能改进吗?或者这只是最佳实践?

I read in the docs that Context.startForegroundService() has an implicit promise that the started service will call startForeground(). However, since Android O is coming out with changes to background and foreground services, are there any other performance improvements that it has compared to using the older startService() method, or is it just best practice going forward?

推荐答案

这既不涉及性能改进,也不涉及收益,也不涉及最佳实践.

It's about neither performance improvements, nor benefits, nor best practice.

从API 26开始,系统不允许后台应用创建后台服务.

Starting from API 26, system just doesn't allow a background app to create a background service.

因此,如果您的应用程序在后台运行(也欢迎您在后台运行,也可以这样做),您 必须 使用Context.startForegroundService(Intent)而不是以前的startService(Intent).然后,该服务必须在启动后的前5秒钟内调用startForeground(int, Notification),否则系统将停止该服务.

So, if your app is in the background (you're welcome to do the same if it's in the foreground as well), you have to to use Context.startForegroundService(Intent) instead of the former startService(Intent). The service must then call startForeground(int, Notification) within first 5 seconds after it has started, otherwise system will stop the service.

还应该提及的是,有信息表明,从后台应用程序使用startService(Intent)启动服务的旧方法在当前版本的Android Oreo上仍然适用,但很快就会得到解决.

It should also be mentioned that there is information that the old way with starting a service with startService(Intent) from a background app still works on the current release of Android Oreo, but it will be fixed soon.

因此,从API 26开始,无论何时要启动前台服务,您都希望使用新的Context.startForegroundService(Intent)方法而不是startService(Intent).

Hence, starting from the API 26, you want to use new Context.startForegroundService(Intent) method instead of startService(Intent) whenever you want to start a foreground service.

这篇关于使用前景服务的Context.startForegroundService(Intent)而不是Context.startService(Intent)有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆