Android/java多线程(四)-IntentService

往期文章:
Android/java 多线程(一)-Thread的使用以及源码分析
Android/java 多线程(二)-Thread的好兄弟Handler
Android/java 多线程(三)-HandlerThread的使用场景及源码解析

简介

一个方便的能在子线程中运行的服务,一个IntentService对应一个线程,由于是四大组件,优先级比线程高,不易被系统回收,处理完任务还能主动回收,因此用来处理后台下载任务极为适合

简单使用

先继承此类,重写必要的方法,我们的逻辑处理是在onHandleIntent()方法中

/**
 * Created by hj on 2018/12/21.
 * 说明:
 */
public class MyIntentService extends IntentService {

    public MyIntentService() {
        super("MyIntentService");
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        //核心方法,处理异步逻辑
        Log.i("HJ","onHandleIntent");
    }

    @Override
    public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
        Log.i("HJ","onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onCreate() {
        Log.i("HJ", "onCreate");
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        Log.i("HJ","onDestroy");
        super.onDestroy();
    }
}

在清单文件中注册:

        
            
                
            
        

在Activity中运行:

        Intent intent = new Intent("com.jay.thread");
        intent.setPackage(getPackageName());
        startService(intent);

打印的结果如下:

2018-12-21 15:03:14.848 4241-4241/com.zj.example.customview.funnel I/HJ: onCreate
2018-12-21 15:03:14.850 4241-4241/com.zj.example.customview.funnel I/HJ: onStartCommand
2018-12-21 15:03:14.850 4241-4258/com.zj.example.customview.funnel I/HJ: onHandleIntent
2018-12-21 15:03:16.225 4241-4241/com.zj.example.customview.funnel I/HJ: onDestroy

可以看到,它的生命周期是onCreate()->onStartCommand()->onHandleIntent()->onDestroy(),而且当它把onHandleIntent()方法里的逻辑处理完毕后会自动调用onDestroy来结束,所以我们不需要主动来关闭它。

而且说到隐式启动Service,这里要叨逼两句,如果我把上面的intent.setPackage(getPackageName())去掉那么在5.0以上机型会遇到这种异常:

 Caused by: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.jay.thread }
        at android.app.ContextImpl.validateServiceIntent(ContextImpl.java:1448)
        at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1489)
        at android.app.ContextImpl.startService(ContextImpl.java:1461)
        at android.content.ContextWrapper.startService(ContextWrapper.java:644)
        at com.zj.example.customview.funnel.MainActivity.onCreate(MainActivity.java:34)
        at android.app.Activity.performCreate(Activity.java:6975)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
        at android.os.Handler.dispatchMessage(Handler.java:105) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6541) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

原因是在5.0源码中谷歌做了限制,如果component和package都为空,那么就会抛出这个异常,详见源码(源码位置:sdk/sources/android21/android/app/ContextImpl.java):

 private void validateServiceIntent(Intent service) {
        if (service.getComponent() == null && service.getPackage() == null) {
            if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) {
                IllegalArgumentException ex = new IllegalArgumentException(
                        "Service Intent must be explicit: " + service);
                throw ex;
            } else {
                Log.w(TAG, "Implicit intents with startService are not safe: " + service
                        + " " + Debug.getCallers(2, 3));
            }
        }
    }

So,我们只要保证其中一个不为空就可以啦.

源码解析

源码还是比较简单,可以先从onCreate()方法看起,具体逻辑见注释:

public abstract class IntentService extends Service {
    private volatile Looper mServiceLooper; //HandlerThread中的Looper
    private volatile ServiceHandler mServiceHandler; //逻辑处理Handler
    private String mName; //线程名称
    private boolean mRedelivery; //是否保证intent在服务被杀死后能被接收到

    //子线程Handler的逻辑处理类
    private final class ServiceHandler extends Handler {
        public ServiceHandler(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
            onHandleIntent((Intent)msg.obj); //onHandleIntent方法回调
            stopSelf(msg.arg1); //自动回收
        }
    }

    public IntentService(String name) {
        super();
        mName = name;
    }

   
   /**
   *此方法的作用:如果为true,则将Service StartResult状态置为START_REDELIVER_INTENT,这样当onHandleIntent方法还未回调的时候服务被回收,重启的时候onHandleIntent方法会被回调,发送上一次回收前的intent,如果有多个intent,将会发送最后一个意图
   *
   **/
    public void setIntentRedelivery(boolean enabled) {
        mRedelivery = enabled;
    }

    @Override
    public void onCreate(){
        super.onCreate();
        //创建HandlerThread 并将线程命名为IntentService+自定义名称的形式
        HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
        thread.start();

        mServiceLooper = thread.getLooper();
        mServiceHandler = new ServiceHandler(mServiceLooper); //将HandlerThread里初始化的looper设置给子线程Handler
    }

    @Override
    public void onStart(@Nullable Intent intent, int startId) {
        Message msg = mServiceHandler.obtainMessage();
        msg.arg1 = startId;
        msg.obj = intent;  //我们在onHandleIntent方法里接收的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(@Nullable Intent intent, int flags, int startId) {
        onStart(intent, startId);
        //StartResult状态设置是在这里
        return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        mServiceLooper.quit();
    }

    //这里将返回值置为了null,所以不建议使用bindService的形式来启动它,如果需要,建议直接使用Service
    @Override
    @Nullable
    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)}.
     *               This may be null if the service is being restarted after
     *               its process has gone away; see
     *               {@link android.app.Service#onStartCommand}
     *               for details.
     */
    @WorkerThread
    protected abstract void onHandleIntent(@Nullable Intent intent);
}

我们在源码中看到,内部实现其实还是HandlerThread+Handler的方式,onCreate()中做了一些初始化的操作,在onStart中将intent用obj的方式传递给了ServiceHandler,然后在handleMessage中拿到intent并调用了onHandleIntent回调出来,这样一个完整的线程有序循环就建立了,而且因为是四大组件,存活率有很大的提升,这是直接用线程实现所没有的优势

看完了源码,有没有对HandlerThread+Handler的应用进一步加深呢?

你可能感兴趣的:(Android/java多线程(四)-IntentService)