【移动开发】Service类onStartCommand()返回值和参数
来源:互联网 发布:oracle数据库字符集 编辑:程序博客网 时间:2024/05/20 14:28
返回值
从Android官方文档中,知道onStartCommand有4种返回值:
http://developer.android.com/reference/android/app/Service.html
/** * 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; /** * 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; /** * 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; /** * 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;
参数
onStartCommand()的函数原型,代码如下:
/** * Called by the system every time a client explicitly starts the service by calling * {@link android.content.Context#startService}, providing the arguments it supplied and a * unique integer token representing the start request. Do not call this method directly. * * <p>For backwards compatibility, the default implementation calls * {@link #onStart} and returns either {@link #START_STICKY} * or {@link #START_STICKY_COMPATIBILITY}. * * <p>If you need your application to run on platform versions prior to API * level 5, you can use the following model to handle the older {@link #onStart} * callback in that case. The <code>handleCommand</code> method is implemented by * you as appropriate: * * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/ForegroundService.java * start_compatibility} * * <p class="caution">Note that the system calls this on your * service's main thread. A service's main thread is the same * thread where UI operations take place for Activities running in the * same process. You should always avoid stalling the main * thread's event loop. When doing long-running operations, * network calls, or heavy disk I/O, you should kick off a new * thread, or use {@link android.os.AsyncTask}.</p> * * @param intent The Intent supplied to {@link android.content.Context#startService}, * as given. This may be null if the service is being restarted after * its process has gone away, and it had previously returned anything * except {@link #START_STICKY_COMPATIBILITY}. * @param flags Additional data about this start request. Currently either * 0, {@link #START_FLAG_REDELIVERY}, or {@link #START_FLAG_RETRY}. * @param startId A unique integer representing this specific request to * start. Use with {@link #stopSelfResult(int)}. * * @return The return value indicates what semantics the system should * use for the service's current started state. It may be one of the * constants associated with the {@link #START_CONTINUATION_MASK} bits. * * @see #stopSelfResult(int) */ public int onStartCommand(Intent intent, int flags, int startId) { onStart(intent, startId); return mStartCompatibility ? START_STICKY_COMPATIBILITY : START_STICKY; }官方解释如下:
public int onStartCommand (Intent intent, int flags, int startId)
Called by the system every time a client explicitly starts the service by calling startService(Intent)
, providing the arguments it supplied and a unique integer token representing the start request. Do not call this method directly.
For backwards compatibility, the default implementation calls onStart(Intent, int)
and returns either START_STICKY
or START_STICKY_COMPATIBILITY
.
If you need your application to run on platform versions prior to API level 5, you can use the following model to handle the older onStart(Intent, int)
callback in that case. The handleCommand
method is implemented by you as appropriate:
// This is the old onStart method that will be called on the pre-2.0// platform. On 2.0 or later we override onStartCommand() so this// method will not be called.@Overridepublic void onStart(Intent intent, int startId) { handleCommand(intent);}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) { handleCommand(intent); // We want this service to continue running until it is explicitly // stopped, so return sticky. return START_STICKY;}
Note that the system calls this on your service's main thread. A service's main thread is the same thread where UI operations take place for Activities running in the same process. You should always avoid stalling the main thread's event loop. When doing long-running operations, network calls, or heavy disk I/O, you should kick off a new thread, or use AsyncTask
.
Parameters
startService(Intent)
, as given. This may be null if the service is being restarted after its process has gone away, and it had previously returned anything except START_STICKY_COMPATIBILITY
. START_FLAG_REDELIVERY
, or START_FLAG_RETRY
. stopSelfResult(int)
. Returns
- The return value indicates what semantics the system should use for the service's current started state. It may be one of the constants associated with the
START_CONTINUATION_MASK
bits.
See Also
stopSelfResult(int)
flags有3种取值,flags值和onStartCommand()的返回值有着直接的关系。如果Service被系统意外终止,重启的时候,传入的flags。官方文档描述:
public static final int START_FLAG_REDELIVERY
This flag is set in onStartCommand(Intent, int, int)
if the Intent is a re-delivery of a previously delivered intent, because the service had previously returned START_REDELIVER_INTENT
but had been killed before calling stopSelf(int)
for that Intent.
public static final int START_FLAG_RETRY
This flag is set in onStartCommand(Intent, int, int)
if the Intent is a a retry because the original attempt never got to or returned from onStartCommand(Intent, int, int)
.
- 【移动开发】Service类onStartCommand()返回值和参数
- 【移动开发】Service类onStartCommand()返回值和参数
- Service onStartCommand的参数和返回值的含义
- Service类onStartCommand()返回值讲解.
- Service类onStartCommand()返回值讲解.
- Android:Service类onStartCommand的返回值
- Android中Service类onStartCommand返回值
- Service类onStartCommand()返回值讲解.
- Service类onStartCommand()返回值讲解.
- Service类onStartCommand()的三个返回值
- Service类onStartCommand()的返回值
- Service onStartCommand返回值问题
- Service.onStartCommand() 返回值详解
- Service#onStartCommand返回值解析
- Service onStartCommand返回值问题
- Service生命周期以及Service类onStartCommand的返回值问题
- Android中Service类onStartCommand的返回值问题
- Android中Service类onStartCommand的返回值问题
- php json_decode 转换为空的解决方案一
- ORA-12737: Instant Client Light: unsupported server character set %s
- linux 学习笔记
- Workflow常见错误如何解决
- 【DB.PL/SQL】程序流程控制 —— 异常处理
- 【移动开发】Service类onStartCommand()返回值和参数
- 关注CSDN社区微信,更多精彩等你来
- cocos2d 坐标系使用
- Linux curl命令参数常用
- CreateThread函数
- Eclipse杂记之屏蔽自带的ant插件
- Java报错&解决汇总
- leetcode - Minimum Path Sum
- 编辑器引发的PHP网站问题