TileService 是否被视为前台进程
Is a TileService considered as foreground process
在 Android 中,我们有一些新的 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:
- IME
- Wallpaper service
- Notification listener
- Voice or text service
如果这些条件中的 none 为真,则认为该应用程序处于后台。
AndroidN 中引入的TileService
(用于快速设置磁贴)呢?当我们在 mainfest 文件中将 TileService
注册为 ACTIVE_TILE
时,系统不会在每次磁贴可见时绑定服务(如本 article 中所述),因此我们的服务绑定到另一个应用程序,面对系统进程。
那么我的应用程序(只要将磁贴添加到快速设置窗格中)是否被视为前台应用程序?这会很好,因为我不需要使用这种方法的持久通知,而且用户仍然可以在后台发送我的应用程序(通过删除磁贴)
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.
因此,大多数时候,您的服务将被解除绑定并销毁。
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
可以无限期地以这种方式绑定,我将提交错误报告,因为这是对系统资源的浪费。
在 Android 中,我们有一些新的 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:
- IME
- Wallpaper service
- Notification listener
- Voice or text service
如果这些条件中的 none 为真,则认为该应用程序处于后台。
AndroidN 中引入的TileService
(用于快速设置磁贴)呢?当我们在 mainfest 文件中将 TileService
注册为 ACTIVE_TILE
时,系统不会在每次磁贴可见时绑定服务(如本 article 中所述),因此我们的服务绑定到另一个应用程序,面对系统进程。
那么我的应用程序(只要将磁贴添加到快速设置窗格中)是否被视为前台应用程序?这会很好,因为我不需要使用这种方法的持久通知,而且用户仍然可以在后台发送我的应用程序(通过删除磁贴)
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 theonStartListening()
/onStopListening()
pair of methods.
因此,大多数时候,您的服务将被解除绑定并销毁。
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 foronTileAdded()
andonTileRemoved()
(and for click events). However, the only time you’ll get a callback toonStartListening()
is after you call the staticTileService.requestListeningState()
method. You’ll then be able to update your tile exactly once before receiving a callback toonStopListening()
. 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
可以无限期地以这种方式绑定,我将提交错误报告,因为这是对系统资源的浪费。