文章目录
一、 Service 组件 onStartCommand 方法分析
1、 onStartCommand 函数返回值分析
2、 onStartCommand 函数 START_STICKY_COMPATIBILITY 返回值
3、 onStartCommand 函数 START_STICKY 返回值
4、 onStartCommand 函数 START_NOT_STICKY 返回值
5、 onStartCommand 函数 START_REDELIVER_INTENT 返回值
二、 系统 Service 机制拉活
1、 Service 代码
2、 清单配置
3、启动服务
三、 测试效果
四、 系统 Service 机制拉活总结
五、 源码资源
一、 Service 组件 onStartCommand 方法分析
1、 onStartCommand 函数返回值分析
Service 的生命周期函数 onStartCommand 方法 , 返回一个整型值 ;
Service 中的 mStartCompatibility 标记默认是 false , 因此 onStartCommand 函数默认返回的整型值是 Service.START_STICKY 值 ;
mStartCompatibility 值在 Service 中的 attach 方法中赋值 , 其值为 getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.ECLAIR , 即手机的 API Level 版本号是否小于 5 , 现在肯定没有版本号小于 5 的手机 , 该值默认是 false ;
public abstract class Service extends ContextWrapper implements ComponentCallbacks2, ContentCaptureManager.ContentCaptureClient { /** * 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 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. * @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 @StartResult int onStartCommand(Intent intent, @StartArgFlags int flags, int startId) { onStart(intent, startId); // mStartCompatibility 标记默认是 false , 因此其返回值默认是 START_STICKY return mStartCompatibility ? START_STICKY_COMPATIBILITY : START_STICKY; } /** * @hide */ @UnsupportedAppUsage public final void attach( Context context, ActivityThread thread, String className, IBinder token, Application application, Object activityManager) { attachBaseContext(context); mThread = thread; // NOTE: unused - remove? mClassName = className; mToken = token; mApplication = application; mActivityManager = (IActivityManager)activityManager; mStartCompatibility = getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.ECLAIR; setContentCaptureOptions(application.getContentCaptureOptions()); } @UnsupportedAppUsage private boolean mStartCompatibility = false;
onStartCommand 的返回值有以下几种情况 :
Service.START_STICKY_COMPATIBILITY
Service.START_STICKY
Service.START_NOT_STICKY
Service.START_REDELIVER_INTENT
2、 onStartCommand 函数 START_STICKY_COMPATIBILITY 返回值
Service.START_STICKY_COMPATIBILITY : START_STICKY 兼容版本 , onStartCommand 方法返回该返回值时 , 不能保证服务可以重启 ;
/**
/** * 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;
3、 onStartCommand 函数 START_STICKY 返回值
Service.START_STICKY : onStartCommand 方法返回该 START_STICKY 返回值时 , 如果在执行 onStartCommand 后 , 如果 Service 服务进程被杀掉 , 系统会保留 Service 状态 , 但是不保留启动服务的 Intent ; 之后系统会尝试重新创建该 Service 服务 ; ( 更详细的信息查看下方的源码注释 )
/** * 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;