Android线程和线程池(三)--IntentService

来源:互联网 发布:反马赛克软件torch 编辑:程序博客网 时间:2024/05/17 23:25

IntentService

IntentService是一种特殊的Service,他继承了Service,并能够处理异步请求,是一个抽象类,因此在我们使用的时候必需创建它的子类才能够使用IntentService。IntentService可用于执行后台耗时的任务,当任务执行完成之后会自动停止,同时由于IntentService是服务的原因,和Activity是同级的,因此他的优先级比线程要高很多,因此IntentService比较适合执行一些高优先级的后台任务,因为优先级高,所以不容易被系统杀死。在实现上,IntentService封装了HandlerThread和Handler。

启动IntentService的方式和启动传统的Service一样,每一个耗时操作会以工作队列的方式在IntentService的onHandlerIntent回调方法中执行,并且每次只会执行一个工作线程,执行完第一个再执行第二个。因此如果需要执行后台耗时操作,就省去了我们在Service中手动开线程的麻烦,而且当操作完成后,我们不用手动停止Service。

    @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);    }

这段代码为IntentService源码中的onCreate方法,当IntentService第一次启动时,会调用onCreate方法,我们可以看到,在onCreate方法中创建了一个HandlerThread,并且用它的Looper来构造了一个Handler对象,这样通过mServiceHandler发送的消息就会在HandlerThread中执行。

IntentService在启动的时候会调用onStartCommand方法,又因为IntentService可以多次被启动,因此每次启动的时候都会调用onStartCommand方法,该方法会调用onStart方法:

    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        onStart(intent, startId);        return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;    }    @Override    public void onStart(Intent intent, int startId) {        Message msg = mServiceHandler.obtainMessage();        msg.arg1 = startId;        msg.obj = intent;        mServiceHandler.sendMessage(msg);    }    @Override    public void handleMessage(Message msg) {        onHandleIntent((Intent)msg.obj);        stopSelf(msg.arg1);    }

我们可以看到,在IntentService启动的时候,只是通过mServiceHandler发送了一个消息,该消息包括启动的Intent信息和启动的ID,这个消息会在HandlerThread的方法handleMessage中被处理,而处理仅仅是再次将其转交给onHandleIntent方法,该方法是我们需要具体实现的方法,在处理完成后会调用stopSelf方法来结束。因此如果我们后台有多个任务,即多次启动IntentService,那么消息就会被放入HandlerThread的消息队列中,最终在onHandlerIntent中被处理,当所有任务处理完成之后才会调用stopSelf方法。

下面我们用一个示例演示IntentService的使用:

public class MyIntentService extends IntentService{    public static final String ACTION_FIRSTTASK = "com.zy.action.FIRST_TASK";    public static final String ACTION_SECONDTASK = "com.zy.action.SECOND_TASK";    public static final String ACTION_THIRDTASK = "com.zy.action.THIRD_TASK";    public MyIntentService(){        super("task_action");    }    @Override    public void onCreate() {        super.onCreate();    }    @Override    protected void onHandleIntent(Intent intent) {        //String action = intent.getStringExtra("task_action");        String action = intent.getAction();        if (action.equals(ACTION_FIRSTTASK)){            //do something            System.out.println("Service started.");        }else if (action.equals(ACTION_SECONDTASK)){            //do something        }else if (action.equals(ACTION_THIRDTASK)){            //do something        }        System.out.println(action);        //模拟耗时操作        SystemClock.sleep(3000);    }    @Override    public void onDestroy() {        System.out.println("Service destroyed.");        super.onDestroy();    }}

注意我们必须要重写一个无参的构造函数,如上代码所示。

使用代码如下:

        Intent startServiceIntent = new Intent(this,MyIntentService.class);        startServiceIntent.setAction(MyIntentService.ACTION_FIRSTTASK);        startService(startServiceIntent);        startServiceIntent.setAction(MyIntentService.ACTION_SECONDTASK);        startService(startServiceIntent);        startServiceIntent.setAction(MyIntentService.ACTION_THIRDTASK);        startService(startServiceIntent);

如上我们启动了三次我们定义的IntentService,观察打印结果发现任务按照顺序执行,且所有任务按顺序执行,所有任务执行完毕之后调用onDestroy方法。

如下所示为执行结果:

02-23 21:51:27.858    2338-2338/com.example.zhangyi.dzbg I/System.out﹕ Service created.02-23 21:51:27.859    2338-2352/com.example.zhangyi.dzbg I/System.out﹕ Service started.02-23 21:51:27.859    2338-2352/com.example.zhangyi.dzbg I/System.out﹕ com.zy.action.FIRST_TASK02-23 21:51:30.860    2338-2352/com.example.zhangyi.dzbg I/System.out﹕ com.zy.action.SECOND_TASK02-23 21:51:33.860    2338-2352/com.example.zhangyi.dzbg I/System.out﹕ com.zy.action.THIRD_TASK02-23 21:51:36.862    2338-2338/com.example.zhangyi.dzbg I/System.out﹕ Service destroyed.

注意:因为IntentService也是服务,因此需要我们在AndroidManifest.xml中进行注册才能使用。

0 0
原创粉丝点击