Service中onStartCommand的返回值问题
2015-03-04 10:39
253 查看
Android开发中,每次调用startService(Intent)时都会调用service对象的onStartCommand(Intent intent,int flag,int startId)方法,其返回值是一个常量,有四种类型:
/**
* Constant to return from {@link #onStartCommand}: if this service's
* process is killed while it is started (after returning from
* {@link #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 {@link #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.
*
* <p>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.
*/
public static final int START_STICKY = 1;即当服务被杀死后,保留service的状态为开始状态,不会保留传递的Intent。随后系统会尝试重新创建service,由于服务状态为开始状态,所有创建是会调用onStartCommand方法。如果在此期间没有任何启动命令被传递到service,那么Intent参数为null。
/**
* Constant to return from {@link #onStartCommand}: if this service's
* process is killed while it is started (after returning from
* {@link #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
* {@link Context#startService Context.startService(Intent)}. The
* service will not receive a {@link #onStartCommand(Intent, int, int)}
* call with a null Intent because it will not be re-started if there
* are no pending Intents to deliver.
*
* <p>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 {@link #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.
*/
public static final int START_NOT_STICKY = 2;如果在执行完onStartCommand后,服务被杀死,系统不会自动重启服务。
/**
* Constant to return from {@link #onStartCommand}: if this service's
* process is killed while it is started (after returning from
* {@link #onStartCommand}), then it will be scheduled for a restart
* and the last delivered Intent re-delivered to it again via
* {@link #onStartCommand}. This Intent will remain scheduled for
* redelivery until the service calls {@link #stopSelf(int)} with the
* start ID provided to {@link #onStartCommand}. The
* service will not receive a {@link #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).
*/
public static final int START_REDELIVER_INTENT = 3;重传Intent。如果执行完onStartCommand后被杀死,系统被自动重启服务,并将Intent传入。
/**
* Constant to return from {@link #onStartCommand}: compatibility
* version of {@link #START_STICKY} that does not guarantee that
* {@link #onStartCommand} will be called again after being killed.
*/
public static final int START_STICKY_COMPATIBILITY = 0;START_STICKY的兼容版本。
/**
* Constant to return from {@link #onStartCommand}: if this service's
* process is killed while it is started (after returning from
* {@link #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 {@link #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.
*
* <p>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.
*/
public static final int START_STICKY = 1;即当服务被杀死后,保留service的状态为开始状态,不会保留传递的Intent。随后系统会尝试重新创建service,由于服务状态为开始状态,所有创建是会调用onStartCommand方法。如果在此期间没有任何启动命令被传递到service,那么Intent参数为null。
/**
* Constant to return from {@link #onStartCommand}: if this service's
* process is killed while it is started (after returning from
* {@link #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
* {@link Context#startService Context.startService(Intent)}. The
* service will not receive a {@link #onStartCommand(Intent, int, int)}
* call with a null Intent because it will not be re-started if there
* are no pending Intents to deliver.
*
* <p>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 {@link #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.
*/
public static final int START_NOT_STICKY = 2;如果在执行完onStartCommand后,服务被杀死,系统不会自动重启服务。
/**
* Constant to return from {@link #onStartCommand}: if this service's
* process is killed while it is started (after returning from
* {@link #onStartCommand}), then it will be scheduled for a restart
* and the last delivered Intent re-delivered to it again via
* {@link #onStartCommand}. This Intent will remain scheduled for
* redelivery until the service calls {@link #stopSelf(int)} with the
* start ID provided to {@link #onStartCommand}. The
* service will not receive a {@link #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).
*/
public static final int START_REDELIVER_INTENT = 3;重传Intent。如果执行完onStartCommand后被杀死,系统被自动重启服务,并将Intent传入。
/**
* Constant to return from {@link #onStartCommand}: compatibility
* version of {@link #START_STICKY} that does not guarantee that
* {@link #onStartCommand} will be called again after being killed.
*/
public static final int START_STICKY_COMPATIBILITY = 0;START_STICKY的兼容版本。
相关文章推荐
- Service 的 onStartCommand()方法的返回值问题
- Service onStartCommand返回值问题
- Service onStartCommand返回值问题
- Service onStartCommand 返回值
- android中service的onStartCommand()方法中intent为null的问题
- 【android】在Service的onStartCommand()中调用stopself()应该注意的问题
- Service中onStartCommand方法intent报空的问题
- Android onStartCommand返回值问题
- Service#onStartCommand返回值解析
- Service onStartCommand各种返回详解,解决问题:只调用onCreate不调用onStartCommand
- 【android】在Service的onStartCommand()中调用stopself()应该注意的问题
- Service 的 onStartCommand(Intent, int, int) 返回值
- Service的onStartCommand中报空指针问题
- Service中的onStartCommand()方法的三种返回值的含义以及理解
- Service中onStartCommand回调方法的返回值分析
- Android Service 的 onStartCommand 返回值
- 我的Android进阶之旅------>android中service的onStartCommand()方法中intent为null的问题
- Service onStartCommand各种返回详解,解决问题:只调用onCreate不调用onStartCommand
- Service onStartCommand的参数和返回值的含义
- Android中Service的onStartCommand使用和遇到的问题