Android异步之旅:探索IntentService

1.介绍IntentService


       IntentService是Android中的一个Service类,用于在后台执行耗时操作,而不会阻塞UI线程。它封装了HandlerThread和Handler,使得我们可以方便地在后台执行任务,而不需要自己管理线程和消息处理。

以下是 IntentService 的主要特点和用法:

  1. 自动停止:当所有的请求都被处理完毕后,IntentService 会自动停止,无需手动调用 stopSelf() 方法。

  2. 工作线程:IntentService 会在后台创建一个工作线程来处理请求,因此可以安全地执行长时间运行的任务,而不会阻塞主线程。

  3. 队列处理:IntentService 会按照请求的顺序逐个处理,确保每个请求都能得到处理,不会出现并发问题。

  4. 默认实现:IntentService 已经实现了 onStartCommand() 方法和 Handler,因此开发者只需要实现 onHandleIntent() 方法来处理请求逻辑即可。

2.IntentService源码


总结:

  1. 在onCreate方法中,IntentService创建了一个HandlerThread和Handler对象,并启动HandlerThread,用于执行后台任务。

  2. 在onStartCommand方法中,IntentService会调用onStart方法,并将传入的Intent传递给onStart方法。

  3. 在onStart方法中,会通过Handler对象发送消息,调用onHandleIntent方法来处理传入的Intent,这是一个抽象方法,需要我们自己来实现具体的后台任务逻辑。

  4. 在onDestroy方法中,IntentService会停止HandlerThread,并释放资源。

下面是详细的源码解析:

IntentService类封装了HandlerThread和Handler。

public void onCreate() {
​
    super.onCreate();
    HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
    thread.start();
​
    mServiceLooper = thread.getLooper();
    mServiceHandler = new ServiceHandler(mServiceLooper);
}

当IntentService被第一次启动时,它的onCreate方法会被调用,onCreate方法被创建一个HandlerThread,然后使用它的Looper来创建一个Handler对象mServiceHandler,mServiceHandler发送的消息最终都会在HandlerThread中执行,从这个角度来看,IntentService也可以用于执行后台任务。每次启动IntentService,它的onStartCommand方法就会调用一次,IntentService在onStartCommand中处理每个后台任务的Intent。下面看法就会调用一次,IntentService在onStartCommand中处理每个后台任务的Intent。下面看一下inStartCommand方法是如何处理外界的Intent的,onStartCommand调用了onStart的实现如下:

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

IntentService类中的onDestroy方法如下:

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

在这段代码中,mServiceLooper是一个HandlerThread的实例,quit()方法用于停止HandlerThread的消息循环并释放相关资源。这是为了确保在Service销毁时,后台线程也能够被正确地停止和清理,以避免内存泄漏和资源浪费。 

3.IntentService的简单使用


使用步骤:

步骤1: 定义 Intentservice 的子类,需复写 onHandleIntent() 方法

步骤2:在 Manifest.xml中注册服务

步骤3: 在 Activity 中开启 Service 服务

public class MyIntentService extends IntentService {
    public MyIntentService() {
        super("MyIntentService");
    }
    @Override
    protected void onHandleIntent(Intent intent) {
        String action=intent.getStringExtra("task_action");
        Log.d("xxx","receive task :"+action);
        SystemClock.sleep(300);
        if ("com.example.intentservice_java".equals(action)) {
            Log.d("xxx","handle task:"+action);
        }
    }
    @Override
    public void onDestroy() {
        Log.d("xxx","service onDestroy");
        super.onDestroy();
    }
}

发起三个后台任务请求:

Intent service =new Intent(this,MyIntentService.class);
service.putExtra("task_action","com.example.intentservice_java1");
startService(service);
service.putExtra("task_action","com.example.intentservice_java2");
startService(service);
service.putExtra("task_action","com.example.intentservice_java3");
startService(service);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值