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).



更多相关文章

  1. android调用系统相机返回null的bug
  2. 在系统里设置文件默认打开APP
  3. Android 如何识别判断小米 魅族 华为 系统 MIUI EMUI FLYME
  4. android 系统定制的小技巧(网络收集)
  5. android编译系统 build/core
  6. 如何修改Android5.1系统USB连接方式默认MTP
  7. 跑马灯结合TextSwitcher实现系统公告栏
  8. Android 通过按钮弹出系统菜单(通过Button显示菜单)转
  9. 打开android 系统联系人

随机推荐

  1. android读书知识点总结
  2. android系统提供的常用命令行工具
  3. android PopupWindow模拟Windows开始菜单
  4. Android--打包工具,100个渠道包只需要10秒
  5. Android(安卓)Path详解
  6. Android中使用PULL方式解析XML和XmlSeria
  7. Linux 图形化终端下截图与快捷键实用技巧
  8. Android(安卓)Service用法讲解与实例
  9. SharedPreferences保存对象
  10. Android(安卓)调用系统相机拍照并储存在