gpt4 book ai didi

android - TileService 是否被视为前台进程

转载 作者:行者123 更新时间:2023-11-29 01:07:20 32 4
gpt4 key购买 nike

在 Android O 中我们有一些新的 background limitations .例如,我们只能通过 Context.registerReceiver 方法注册隐式广播。当系统终止我们的进程时(例如由于内存不足),注册的接收器也将被销毁。

为了减少系统杀死我们进程的机会,我们必须告诉系统这个进程仍然在前台。根据 documentation如果满足以下任一条件,则认为应用程序位于前台:

  • It has a visible activity, whether the activity is started or paused.
  • It has a foreground service
  • Another foreground app is connected to the app, either by binding to one of its services or by making use of one of its content providers. For example, the app is in the foreground if another app binds to its:
    1. IME
    2. Wallpaper service
    3. Notification listener
    4. Voice or text service

如果这些条件都不成立,则认为该应用程序处于后台。

Android N 中引入的 TileService(用于快速设置磁贴)怎么样?当我们在 mainfest 文件中将 TileService 注册为 ACTIVE_TILE 时,系统不会在每次 tile 可见时绑定(bind)服务(如本 article 中所述),因此我们的服务绑定(bind)到另一个应用程序,面向系统进程。

那么我的应用程序(只要将磁贴添加到快速设置 Pane 中)是否被视为前台应用程序?这会很好,因为我不需要使用这种方法的持久通知,而且用户仍然可以在后台发送我的应用程序(通过删除磁贴)

最佳答案

So is my application (as long as the tile is added to the quick settings pane) considered as foreground application?

通常不会。引用您链接到的文章:

It is important to note that your TileService will mostly certainly be unbound (and destroyed) when the user is not viewing the tile — don’t assume that your Service will be alive outside of the onStartListening()/onStopListening() pair of methods.

因此,大多数时候,您的服务将被解除绑定(bind)和销毁。


the system does not bind the service every time the tile becomes visible (as noted in this article)

我猜你指的是这些段落:

In active mode, your TileService will still be bound for onTileAdded() and onTileRemoved() (and for click events). However, the only time you’ll get a callback to onStartListening() is after you call the static TileService.requestListeningState() method. You’ll then be able to update your tile exactly once before receiving a callback to onStopListening(). This gives you an easy one-shot ability to update your tile right when your data changes whether the tile is visible or not.

Since active tiles don’t have to be bound every time the tile becomes visible, active tiles are better for system health. Building active tiles means less processes the system needs to bind to every time the quick settings panel becomes visible. (Of course, the system already limits the amount of bound TileServices based on available memory, etc, but by then you’re already near the border of memory thrashing — not where you want to be.)

我希望有某种超时机制:如果您调用 requestListeningState(),那么在 X 秒内不更新磁贴,您将被调用 onStopListening() 。聆听状态不持久;它只存在一次更新。因此,系统应该及时更新。我将测试这种情况,如果 TileService 可以无限期地以这种方式绑定(bind),我将提交错误报告,因为这是对系统资源的浪费。

关于android - TileService 是否被视为前台进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45926196/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com