Android Service 中 onStartCommand()函数返回值含义

onStartCommand()是由Android系统调用的,本质上也是调用了onStart()方法。

onStartCommand()返回值有几种:

1)START_STICKY

英文解释:

Constant to return from onStartCommand: if this service‘s process is killed while it is started (after returning from
onStartCommand), then leave it in the started state but don‘t retain this delivered intent. Later the system will try to re-create the service. Because it is in the started state, it will guarantee to call
onStartCommand after creating the new service instance; if there are not any pending start commands to be delivered to the service, it will be called with a null intent object, so you must take care to check for this.

This mode makes sense for things that will be explicitly started and stopped to run for arbitrary periods of time, such as a service performing background music playback.

说明:当返回值是START_STICKY时,service只会在我们想要它结束运行的时候停止运行。即使service所在进程被kill掉了,系统也会重新创建一个新的service。

2)START_STICKY_COMPATIBILITY

compatibility version of START_STICKY that does not guarantee that
onStartCommand will be called again after being killed.

说明:START_STICKY的兼容版本,但是不保证被kill掉以后一定会重启

3)START_NOT_STICKY

Constant to return from onStartCommand: if this service‘s process is killed while it is started (after returning from
onStartCommand), and there are no new start intents to deliver to it, then take the service out of the started state and don‘t recreate until a future explicit call to
Context.startService(Intent). The service will not receive a
onStartCommand(Intent, int, int) call with a null Intent because it will not be re-started if there are no pending Intents to deliver.

This mode makes sense for things that want to do some work as a result of being started, but can be stopped when under memory pressure and will explicit start themselves again later to do more work. An example of such a service would be one that polls for
data from a server: it could schedule an alarm to poll every N minutes by having the alarm start its service. When its
onStartCommand is called from the alarm, it schedules a new alarm for N minutes later, and spawns a thread to do its networking. If its process is killed while doing that check, the service will not be restarted until
the alarm goes off.

说明:和START_STICKY作用相反,在被kil掉之后,不会重启新的service。适用于那些在系统资源(如memory)宽裕的情况下能run,在资源不宽裕(如内存不够)的情况下能stop的任务。

4)START_REDELIVER_INTENT

Constant to return from onStartCommand: if this service‘s process is killed while it is started (after returning from
onStartCommand), then it will be scheduled for a restart and the last delivered Intent re-delivered to it again via
onStartCommand. This Intent will remain scheduled for redelivery until the service calls
stopSelf(int) with the start ID provided to
onStartCommand. The service will not receive a
onStartCommand(Intent, int, int) call with a null Intent because it will will only be re-started if it is not finished processing all Intents sent to it (and any such pending events will be delivered at the point
of restart).

时间: 2024-10-09 21:00:03

Android Service 中 onStartCommand()函数返回值含义的相关文章

【Android】Android中Service类onStartCommand的返回值有关问题(转)

@Override public int onStartCommand(Intent intent, int flags, int startId) { System.out.println("---------->>onStartCommand2"); return super.onStartCommand(intent, flags, startId); } Android开发的过程中,每次调用startService(Intent)的时候,都会调用该Service对象

Android中Service类onStartCommand的返回值问题

Android开发的过程中,每次调用startService(Intent)的时候,都会调用该Service对象的onStartCommand(Intent,int,int)方法,然后在onStartCommand方法中做一些处理.然后我们注意到这个函数有一个int的返回值,这篇文章就是简单地讲讲int返回值的作用. 从Android官方文档中,我们知道onStartCommand有4种返回值: START_STICKY:如果service进程被kill掉,保留service的状态为开始状态,但

Service 中onStartCommand方法参数的含义

在Service中onStartCommand(Intent intent, int flags, int startId)这三个参数的含义分别是,intent就是startService(Intent intent)中的intent;flags代表flags表示启动服务的方式: Additional data about this start request. Currently either 0, START_FLAG_REDELIVERY, or START_FLAG_RETRY. STA

linux编程中接收主函数返回值以及错误码提示

程序A创建子进程,并调用进程B,根据不调用的不同情况,最后显示结果不同. #include <stdio.h> #include <unistd.h> #include <sys/wait.h> #include <sys/types.h> #include <errno.h> int main() { pid_t pid, rpid; int stat; if ((pid = fork()) < 0) { perror("for

string 中的 length函数 和size函数 返回值问题

string 中的 length函数 和 size函数 的返回值  (  还有 char [ ] 中 测量字符串的  strlen 函数 ) 应该是 unsigned int 类型的 不可以 和 -1 比较. 应尽量避免 unsigned int 类型 和 int类型 数据 的比较 .当unsigned int 类型 和 int类型 数据 比较 时 ,会 把int 类型 转换 为 unsigned int类型 .如果 int是负数 ,转换 为 unsigned int 会是 一个 很大 的正整数

自定义函数中的参数返回值 “-&gt; (Int -&gt; Int)”的问题

func makeIncrementer() -> (Int -> Int) { func addOne(number: Int) -> Int { return 1 + number } return addOne } var increment = makeIncrementer() println(increment(7)) 这里为什么要写两个 Int->Int 这里是返回值是参数,左边是参数,右边是返回值的意思. 自定义函数中的参数返回值 "-> (Int

C语言中函数返回值的问题

c语言中有关于在函数返回值的问题,在函数中的局部变量主要是在栈上开辟的,出了函数变量就被回收了,针对函数返回值得问题,给出下面几个比较具体的例子来说明: 函数返回值是在函数中定义的局部变量 这类型的返回值在主函数中是可以使用的,因为返回局部变量值得时候,返回的是值得一个副本,而在主函数中我们需要的也只是这个值而已,因此是可以的,例如 int fun(char *arr) { int num = 0; while (*arr != '\\0') { num = num * 10 + *arr -

Golang中interface{}作为函数参数和函数返回值的使用

package main import (     "errors"     "fmt" ) type item struct {     Name string } func (i item) String() string {     return fmt.Sprintf("item name: %v", i.Name) } type person struct {     Name string     Sex  string } func

Python函数中的变量和函数返回值

1.函数的变量 局部变量和全局变量: Python中的任何变量都有特定的作用域 在函数中定义的变量一般只能在该函数内部使用,这些只能在程序的特定部分使用的变量我们称之为局部变量 在一个文件顶部定义的变量可以供文件中的任何函数调用,这些可以为整个程序所使用的变量称为全局变量. def fun(): x=100 print x fun() x = 100 def fun(): global x   //声明 x +=1 print x fun() print x 外部变量被改: x = 100 de