(2)直接創建一個工作隊列,將一個意圖傳遞給你onHandleIntent()的實現,所以我們就永遠不必擔心多線程。
(3)當請求完成後自己會調用stopSelf(),所以你就不用調用該方法了。
(4)提供的默認實現onBind()返回null,所以也不需要重寫這個方法。so easy啊
(5)提供了一個默認實現onStartCommand(),將意圖工作隊列,然後發送到你onHandleIntent()實現。
我們需要做的就是實現onHandlerIntent()方法,還有一點就是經常被遺忘的,構造函數是必需的,而且必須調用超IntentService(字符串) ,因爲工作線程的構造函數必須使用一個名稱。如何實現呢,我們藉助於谷歌官方文檔來看一下吧。
- public class HelloIntentService extends IntentService {
- /**
- * A constructor is required, and must call the super IntentService(String)
- * constructor with a name for the worker thread.
- */
- public HelloIntentService() {
- super("HelloIntentService");
- }
- /**
- * The IntentService calls this method from the default worker thread with
- * the intent that started the service. When this method returns, IntentService
- * stops the service, as appropriate.
- */
- @Override
- protected void onHandleIntent(Intent intent) {
- // Normally we would do some work here, like download a file.
- // For our sample, we just sleep for 5 seconds.
- long endTime = System.currentTimeMillis() + 5*1000;
- while (System.currentTimeMillis() < endTime) {
- synchronized (this) {
- try {
- wait(endTime - System.currentTimeMillis());
- } catch (Exception e) {
- }
- }
- }
- }
- }
那麼它爲什麼不用stopself()方法呢,我們看一下自身的源代碼把
- public abstract class IntentService extends Service {
- private volatile Looper mServiceLooper;
- private volatile ServiceHandler mServiceHandler;
- private String mName;
- private boolean mRedelivery;
- private final class ServiceHandler extends Handler {
- public ServiceHandler(Looper looper) {
- super(looper);
- }
- @Override
- public void handleMessage(Message msg) {
- onHandleIntent((Intent)msg.obj);
- stopSelf(msg.arg1);
- }
- }
- /**
- * Creates an IntentService. Invoked by your subclass's constructor.
- *
- * @param name Used to name the worker thread, important only for debugging.
- */
- public IntentService(String name) {
- super();
- mName = name;
- }
- /**
- * Sets intent redelivery preferences. Usually called from the constructor
- * with your preferred semantics.
- *
- * <p>If enabled is true,
- * {@link #onStartCommand(Intent, int, int)} will return
- * {@link Service#START_REDELIVER_INTENT}, so if this process dies before
- * {@link #onHandleIntent(Intent)} returns, the process will be restarted
- * and the intent redelivered. If multiple Intents have been sent, only
- * the most recent one is guaranteed to be redelivered.
- *
- * <p>If enabled is false (the default),
- * {@link #onStartCommand(Intent, int, int)} will return
- * {@link Service#START_NOT_STICKY}, and if the process dies, the Intent
- * dies along with it.
- */
- public void setIntentRedelivery(boolean enabled) {
- mRedelivery = enabled;
- }
- @Override
- public void onCreate() {
- // TODO: It would be nice to have an option to hold a partial wakelock
- // during processing, and to have a static startService(Context, Intent)
- // method that would launch the service & hand off a wakelock.
- super.onCreate();
- HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
- thread.start();
- mServiceLooper = thread.getLooper();
- mServiceHandler = new ServiceHandler(mServiceLooper);
- }
- @Override
- public void onStart(Intent intent, int startId) {
- Message msg = mServiceHandler.obtainMessage();
- msg.arg1 = startId;
- msg.obj = intent;
- mServiceHandler.sendMessage(msg);
- }
- /**
- * You should not override this method for your IntentService. Instead,
- * override {@link #onHandleIntent}, which the system calls when the IntentService
- * receives a start request.
- * @see android.app.Service#onStartCommand
- */
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- onStart(intent, startId);
- return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;
- }
- @Override
- public void onDestroy() {
- mServiceLooper.quit();
- }
- /**
- * Unless you provide binding for your service, you don't need to implement this
- * method, because the default implementation returns null.
- * @see android.app.Service#onBind
- */
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- /**
- * This method is invoked on the worker thread with a request to process.
- * Only one Intent is processed at a time, but the processing happens on a
- * worker thread that runs independently from other application logic.
- * So, if this code takes a long time, it will hold up other requests to
- * the same IntentService, but it will not hold up anything else.
- * When all requests have been handled, the IntentService stops itself,
- * so you should not call {@link #stopSelf}.
- *
- * @param intent The value passed to {@link
- * android.content.Context#startService(Intent)}.
- */
- protected abstract void onHandleIntent(Intent intent);
- }
我們可以看到源代碼裏頭的第15行handlerMessage方法裏當處理完請求後就會調用stopself()方法了,外界就不用調用了,此外還有一點我們可以看到代碼最後一行第110行,onhandleIntent()是一個抽象類,而其他類都是抽象類,所以我們就可以理解爲什麼只需要重寫onhandleIntent()方法了吧。