Andorid中的线程形态:IntentService

在

  • 1

    IntentService是特殊的service,它继承service并且是一个抽象类,因此必须创建它的子类才能使用IntentService.这里比如其子类:MyIntentService。比较适合执行一些高优先级的后台耗时工作,因为它不仅使线程还是服务。不容易被系统杀死,事实上IntentService封装了HandlerThread和Handler。当任务执行完毕后,IntentService会自动退出。

  • 2

    当IntentService第一次启动时,它的onCreate方法会调用。源码分析

    onCreate(){
    //创建HandlerThread  
    HandlerThread thread = new HandlerThread("");
    Thread.start();
    //使用HandlerThread的Looper来构造一个Handler对象mServiceHandler
    mServiceLooper = thread.getLooper();
    mServiceHandler = new ServiceHandler(mServiceLooper);
    }
    
    通过1构造了2的对象,这样handler发送的消息都会在HandlerThread中执行

    这里写图片描述

  • 3

    之后每次启动IntentService,它的onStartCommand方法就会调用一次,IntentService在onStartCommand中处理每个后台任务的Intent,而onStartCommand调用了onStart

    public void onStart(Intent intent,int startId){
    Message msg = mServiceHandler.obtainMessage();
    msg.arg1= startId;
    msg.obj = intent;
    mServicehandler.sendMessage(msg);
    }

    这个intent对象的内容和startService(intent)中intent是完全一致的。

  • 4

    mServicehandler收到消息后,会将intent对象传递给onHandleIntent方法去处理

    private final class ServiceHandler extends Handler {
       // ......
       Public ServiceHandler(Looper looper){
         super(looper) }
      //这也可以说明为什么使用looer创建handler对象
    
        @Override
        public void handleMessage(Message msg) {
        onHandleIntent((Intent)msg.obj);
        stopSelf(msg.arg1);
     }
    }
  • 5

    IntentService的onHandleIntent是一个抽象方法,需要在子类中去实现,它的作用是从Intent参数中区分具体的任务并执行,如果目前只有一个后台任务,那么onHandleIntent方法执行完后,stopSelf(int startId)就会直接停止服务;如果目前存在多个后台任务的话,那么当onHandleIntent方法执行完最后一个任务时,stopSelf(int startId)才会直接停止服务。

  • 6

    每执行一次后台任务就启动一次Intentservice,而IntentService内部最终通过sendMessage向HanlderThread发送执行任务,Handler中的Looper是顺序处理消息的,也就是说IntentSerive也是顺序执行后台任务的

  • 7

    代码示例: MainActivity.java

    Intent service = new Intent(this,MyIntentService.class);
    service.putExtra("task_action","com.example.administrator.TASK1");
    startService(service);
    service.putExtra("task_action","com.example.administrator.TASK2");
    startService(service);
    service.putExtra("task_action","com.example.administrator.TASK3");
    startService(service);
    
    
    
    MyIntentService.java
    
    public class MyIntentService extends IntentService {
    private static final String TAG = "MyIntentService";
    public MyIntentService() {
        super(TAG);
    }
    
    @Override
    protected void onHandleIntent(Intent intent) {
        String action = intent.getStringExtra("task_action");
        Log.d(TAG,"receive task :" + action);
        SystemClock.sleep(3000);
        if ("com.example.administrator.TASK1".equals(action)){
            Log.d(TAG,"handle task :" + action);
        }
    }
    
    @Override
    public void onDestroy() {
        Log.d(TAG,"service destroyed");
        super.onDestroy();
    }
    }
    

    console:
    这里写图片描述

  • 通过SystemClock.sleep(3000)来休眠从而来模拟一种耗时的后台任务

  • 执行完三个任务后,打印了service destroyed,说明当任务执行完毕后,IntentService会自动退出。
  • 从打印的时间看来,三个后台任务时排队执行的,执行的顺序与他们发起请求的顺序是一致的,当Task3执行完后,MyIntentService才是真正的停止.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值