zoukankan      html  css  js  c++  java
  • Android Service服务源码相关分析

    一、onStartCommand(Intent intent, int flags, int startId)

    位置:/frameworks/base/core/java/android/app/Service.java

      /**
         * 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;
        }
    【START_STICKY】:如果service进程被kill掉,保留service的状态为开始状态,但不保留递送的intent对象。随后系统会尝试重新创建service,由于服务状态为开始状态,
    所以创建服务后一定会调用onStartCommand(Intent,int,int)方法。如果在此期间没有任何启动命令被传递到service,那么参数Intent将为null。
    
    【START_NOT_STICKY】:“非粘性的”。使用这个返回值时,如果在执行完onStartCommand后,服务被异常kill掉,系统不会自动重启该服务。
    
    【START_REDELIVER_INTENT】:重传Intent。使用这个返回值时,如果在执行完onStartCommand后,服务被异常kill掉,系统会自动重启该服务,并将Intent的值传入。
    
    【START_STICKY_COMPATIBILITY】:START_STICKY的兼容版本,但不保证服务被kill后一定能重启。
    
  • 相关阅读:
    python关于字典嵌套字典,列表嵌套字典根据值进行排序
    Linux安装pycharm并添加图标到桌面
    python_requests ~爬虫~小视频~~~
    Python如何实现doc文件转换为docx文件?
    python同时取每个列表的第一个元素
    python根据字典的值进行排序:
    lambda 函数的用法
    python的拷贝方式以及深拷贝,浅拷贝详解
    python实现快速排序
    python不使用系统库中的排序方法判断一个数组是否是有序数组
  • 原文地址:https://www.cnblogs.com/Miami/p/5607484.html
Copyright © 2011-2022 走看看