Service START_NOT_STICKY、START_STICKY

/**
 * Bits returned by {@link #onStartCommand} describing how to continue
 * the service if it is killed.  May be {@link #START_STICKY},
 * {@link #START_NOT_STICKY}, {@link #START_REDELIVER_INTENT},
 * or {@link #START_STICKY_COMPATIBILITY}.
 */

【译】onStartCommand的返回值(Bits-位?),用来描述如果服务被杀死后,后续如何执行,可选值:START_STICKY、START_NOT_STICKY、START_REDELIVER_INTENT、START_STICKY_COMPATIBILITY
public static final int START_CONTINUATION_MASK = 0xf;

/**
 * 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.
 */

【译】onStartCommand返回的常量:START_STICKY的通用选项,该选项不能保证,服务被杀死后onStartCommand会被重新调用
public static final int START_STICKY_COMPATIBILITY = 0;

/**
 * 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.
 *
 *

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.
 */

【译】onStartCommand返回的常量:如果服务被启动(从onStartCommand返回后)后,服务所在进程被杀死,此时保持服务的启动状态,但是不保持服务之前的Intent。稍后系统会尝试重新创建该服务。因为该服务目前是启动状态,因此,创建新的服务实例后,一定会调用onStartCommand;当没有将要传递给服务启动命令时,服务将会被唤醒并附带空的Intent对象,请注意检查该Intent是否为空。

这种模式适合有明确的启动时间,执行不确定时间周期后会停止的情况,例如,用来进行音乐后台播放的服务。

【解释】被杀后自动重启,保持启动状态,不保持Intent,重新调用onStartCommand,无新Intent则为空Intent—杀死重启后,不继续执行先前任务,能接受新任务
public static final int START_STICKY = 1;

/**
 * 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.
 *
 *

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.
 */

【译】onStartCommand返回的常量:如果服务被启动(从onStartCommand返回后)后,服务所在进程被杀死,并且当前没有新的Intents等待发送给它,那么不再新建服务,并被标记为非启动状态,直至下一个明确启动调用—startService()。因为如果没有新的Intents需要发送,服务不会重启,服务不会收到一个包含空Intent的onStartCommand调用。

这种模式适合启动后做一些任务,内存压力大时,能被停止,后续可以再次明确启动它们做更多任务。一种该类型服务举例:从服务器轮询数据,设置一个每N分钟能够启动服务轮询数据的闹钟,当闹钟调用onStartCommand时,会新设定一个N分钟后启动的闹钟,并生成一个线程处理网络数据。如果服务所在进程在处理网络数据时被杀死,服务在这个闹钟结束前不会重启

【解释】被杀后不重启,不保持启动状态,可以随时停止,适合定时数据轮询场景
public static final int START_NOT_STICKY = 2;

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

【译】onStartCommand返回的常量:如果服务被启动(从onStartCommand返回后)后,服务所在进程被杀死,服务会被重启,并将上次的Intent通过onStartCommand重新传递。这个Intent将会一直保持用来传递,直至服务使用提供给onStartCommand的ID调用stopSelf()。因为如果不是尚有未处理完的Intent(在重启时,所有这些Intent都会被发送),服务不会重启,因此服务不会收到带有空Intent的onStartCommand调用。

【解释】如果有未处理完的Intent,被杀后会重启,并在重启后发送所有Intent。stopSelf后释放保持的Intent。
public static final int START_REDELIVER_INTENT = 3;

你可能感兴趣的:(Android)