【发布时间】:2018-01-02 12:43:05
【问题描述】:
现在更新了 SDK 版本服务类有覆盖方法
int onStartCommand (Intent intent, int flags, int startId)
& 它已取代 Android 2.0(API 级别 5)引入的以下内容。
void onStart(Intent intent, int startid)
我的问题是:
- flag 和 startId 等参数作为参数传递有什么用?
- 我们如何操作这些参数?
更新 1. 2:清除:)
按照 snapcode 清除 flags 参数的用例。默认它接收为 0,如果您的服务重新启动,那么它将收到我们从 onStartCommand(..) 方法返回的标志。
void readFlags(int flags) {
switch (flags) {
case START_FLAG_REDELIVERY:
case START_FLAG_RETRY:
// restarted by system, might be kill app form stack.
break;
default:
// on regular startService call from client.
}
}
和startID非常清楚,每当你调用stopSelf你应该用这个startID调用,所以如果服务有多个客户端的运行请求那么它不会杀死服务,它会简单地停止为这个 startID 工作。它由系统生成,无需操作:)。
3. 但是如何管理这个 startID 来调用 stopSelf 仍然是一个问题?随便一个!!
【问题讨论】:
-
您是否阅读了有关这些参数的 Android 文档?
-
yes..definitely 但没有使用标志和 startId。以及我们如何操纵?