IntentService

  • 基本用法:
    public class MyIntentService extends IntentService {
        private static final String TAG = MyIntentService.class.getCanonicalName();
    
        /**
         * Creates an IntentService.  Invoked by your subclass's constructor.
         *
         * @param name Used to name the worker thread, important only for debugging.
         */
        public MyIntentService(String name) {
            super(name);
        }
        public MyIntentService() {
            this("my");
            setIntentRedelivery(true);
        }
    
        @Override
        protected void onHandleIntent(@Nullable Intent intent) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Bundle bundle = intent.getExtras();
            Student s =  bundle.getParcelable("Person");
            Log.i(TAG, "onHandleIntent: "+s.toString());
    
            Log.i(TAG, "onHandleIntent: threadId"+Thread.currentThread().getId());
            SystemClock.sleep(2000);
            Log.i(TAG, "onHandleIntent: threadId"+Thread.currentThread().getId());
    
        }
    }
    

      需要在清单文件中注册该service

  • 调用的时候只需要startservice,不用管stop,因为该servcie会自己停止
  • 看IntentSercie源码可知,HandlerThread+Handler实现的,
    onHandleIntent 是在子线程运行的方法
  • 	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() {
        
    
            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();
        }
    
    
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    
    
        protected abstract void onHandleIntent(Intent intent);
    }
    

      

posted on 2019-02-27 13:35  endian11  阅读(89)  评论(0编辑  收藏  举报

导航