美文网首页
Android IntentService分析

Android IntentService分析

作者: 晴天12345 | 来源:发表于2017-10-16 23:53 被阅读2次

    1.1 IntentService相关类图

    IntentService相关类图
    • IntentService是一个抽象类,通过继承此类重写onHandlerIntent实现业务逻辑;
    • 业务在HandlerThread线程处理执行;
    • ServiceHandlerHandlerThread持有同一个MessageQueue;
    • ServiceHandler生命周期和IntentService一致;

    2.1 IntentService::onCreate

    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(); // 2.2
    
        mServiceLooper = thread.getLooper(); 
        mServiceHandler = new ServiceHandler(mServiceLooper); // ServiceHandler持有HandlerThread所在线程的MessageQueue
    }
    

    2.2 HandlerThread::run

    int mPriority = Process.THREAD_PRIORITY_DEFAULT;
    public void run() {
        mTid = Process.myTid();
        Looper.prepare();
        synchronized (this) {
            mLooper = Looper.myLooper();
            notifyAll(); // 消息机制创建完成之后,通知HandlerThread可以获取Looper
        }
        Process.setThreadPriority(mPriority);
        onLooperPrepared();
        Looper.loop();
        mTid = -1;
    }
    

    HandlerThread所在线程创建Handler消息机制;

    2.3 Service生命周期

    Service生命周期
    Service生命周期可知,下一步应该是onStartCommand;

    2.4 IntentService::onStartCommand

    public int onStartCommand(Intent intent, int flags, int startId) {
        onStart(intent, startId);
        return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;
    }
    

    2.5 IntentService::onStart

    public void onStart(Intent intent, int startId) {
        // 通过Handler消息池创建消息,并且Message默认Handler为ServiceHandler
        Message msg = mServiceHandler.obtainMessage(); 
        msg.arg1 = startId;
        msg.obj = intent;
        mServiceHandler.sendMessage(msg); // 2.6
    }
    

    2.6 ServiceHandler::handleMessage

    public void handleMessage(Message msg) {
        onHandleIntent((Intent)msg.obj); // 抽象方法
        stopSelf(msg.arg1); // 任务完成之后通知AMS停止当前service
    }
    

    3.1 IntentService::onDestroy

    public void onDestroy() {
        mServiceLooper.quit(); // Looper退出
    }
    

    相关文章

      网友评论

          本文标题:Android IntentService分析

          本文链接:https://www.haomeiwen.com/subject/nhkruxtx.html