如何在 Android TV Oreo 中始终 运行 service/daemon?

How to run a service/daemon in Android TV Oreo at all times?

我想为 Android 电视平台构建一个应用程序。此应用程序的一部分是 service/daemon,它必须在设备启动时启动,并且在 Android 电视开机时必须始终 运行,即使处于待机状态。

为什么我要一直 service/daemon 到 运行?我项目的一部分是便携式设备(例如智能手机 and/or 平板电脑)的应用程序,它将通过 TCP 将命令发送到 Android 电视应用程序。根据 TCP 消息的种类,应用程序将执行操作(电源设备 on/off、将我的应用程序推送到前台等)。

我尝试了不同的代码示例,但 Android TV Oreo 只是在一段时间后终止了此服务。我认为这些代码示例是在 Android 奥利奥限制之前制作的。

用于 Android TV 的 Kodi 等应用程序有一个始终在后台 运行ning 的网络服务。即使是奥利奥。但我不知道他们是怎么做到的。有人能给我一些建议吗?

编辑:Yatse Remote Starter 为 Kodi 做我想做的事,我想用我的应用做的事。它在启动时启动,运行s 即使在待机状态下也是如此。 https://play.google.com/store/apps/details?id=tv.yatse.android.remotestarter&hl=en。我仍然想知道如何,所以我也可以自己做。提前致谢!

许多 android 应用和服务同时 运行。为了降低导致用户体验不佳的问题的可能性,Android 8.0 应用程序有 two ways to limit what an app can do:

Background Service Limitations: While an app is idle, there are limits to its use of background services. This does not apply to foreground services, which are more noticeable to the user.

Broadcast Limitations: With limited exceptions, apps cannot use their manifest to register for implicit broadcasts. They can still register for these broadcasts at runtime, and they can use the manifest to register for explicit broadcasts targeted specifically at their app.

因此,您需要创建一个 ForegroundService 才能继续处理您的应用程序。您可以查看此 SO post 关于此问题。