Android IntentService快速使用

本文深入解析Android中的IntentService,一种用于处理异步请求的服务组件。详细介绍了其使用方法、生命周期及注意事项,帮助开发者更好地理解和运用IntentService。

背景/简介

  • 在Android开发中,凡是遇到耗时操作通常都会交给Service去做,比如上传文件,下载文件等需要耗时的任务。出于对内存的考虑,如果担心Service被杀,通常还能通过startForeground(int, Notification) 将服务升级为前台服务,提升优先级。

  • Service默认是执行在主线程上的,所以,在Service里面不能直接做耗时操作,否者回造成ANR异常,所以通常需要在服务启动后,开启子线程去做一些事情,自己去管理Service的生命周期以及子线程并非是个优雅的做法,所以Android又提供了IntentService。

使用

1.新建一个Service继承IntentService

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;


public class MyIntentService extends IntentService {

    /**
     * 构造
     */
    public MyIntentService() {
        super("MyIntentService");//这里需要传入字符串,标识IntentService子线程name
    }


    /**
     * 处理业务
     * @param intent
     */
    @Override
    protected void onHandleIntent(Intent intent) {
         // doSomething
        Log.d("MyIntentService","onHandleIntent start");
        int i = 0;
        while(i < 10){
            Log.d("MyIntentService","当前: " + i);

            i++;
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        Log.d("MyIntentService","onHandleIntent end");
    }

    /**
     * 服务创建
     */
    @Override
    public void onCreate() {
        super.onCreate();
        //init something
        Log.d("MyIntentService","onCreate");
    }

    /**
     * 服务销毁
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        //release something
        Log.d("MyIntentService","onDestroy");
    }
}

2.开启这个服务

Intent intent = new Intent(MainActivity.this,MyIntentService.class);
intent.putExtra("test","hello");
startService(intent);

然后看日志输出:

01-23 11:51:13.728 11586-11586/first.keller.com.myapplication D/MyIntentService: onCreate
01-23 11:51:13.729 11586-11687/first.keller.com.myapplication D/MyIntentService: onHandleIntent start
01-23 11:51:13.729 11586-11687/first.keller.com.myapplication D/MyIntentService: 当前: 0
01-23 11:51:14.230 11586-11687/first.keller.com.myapplication D/MyIntentService: 当前: 1
01-23 11:51:14.732 11586-11687/first.keller.com.myapplication D/MyIntentService: 当前: 2
01-23 11:51:15.233 11586-11687/first.keller.com.myapplication D/MyIntentService: 当前: 3
01-23 11:51:15.735 11586-11687/first.keller.com.myapplication D/MyIntentService: 当前: 4
01-23 11:51:16.237 11586-11687/first.keller.com.myapplication D/MyIntentService: 当前: 5
01-23 11:51:16.739 11586-11687/first.keller.com.myapplication D/MyIntentService: 当前: 6
01-23 11:51:17.241 11586-11687/first.keller.com.myapplication D/MyIntentService: 当前: 7
01-23 11:51:17.743 11586-11687/first.keller.com.myapplication D/MyIntentService: 当前: 8
01-23 11:51:18.244 11586-11687/first.keller.com.myapplication D/MyIntentService: 当前: 9
01-23 11:51:18.746 11586-11687/first.keller.com.myapplication D/MyIntentService: onHandleIntent end
01-23 11:51:18.749 11586-11586/first.keller.com.myapplication D/MyIntentService: onDestroy

3.注意

  • IntentService 的onHandleIntent(Intent intent) 方法默认是在子线程上的,所以不需要重新开线程;

  • 任务执行完成后,服务会自动销毁,所以通常无需手动停止服务;

  • 不建议使用bind的方式开启IntentService;

  • 如果任务没有执行完,手动停止了服务,会调用onDestroy()方法那么onHandleIntent()方法里的逻辑可能会不停,直到线程结束;

  • 如果连续启动了IntentService,那么任务会在第一次完成之后,继续进行第二次,直到执行够启动服务的次数,服务结束;

  • 如果连续启动了IntentService,第一次执行过程中,停止了服务,那么第二次启动的任务也不会再执行;
  • IntentService受 Android 8.0(API级别26)强加的所有 后台执行限制的约束。在大多数情况下,最好使用JobIntentService,在Android 8.0或更高版本上运行时使用JobIntentService;
  • IntentService 一次只处理一个Intent,逻辑运行的子线程上,不影响主线程,如果此代码需要很长时间,它将阻止对同一个IntentService的其他请求,但它不会阻止任何其他内容。处理完所有请求后,IntentService会自行停止,因此不应该调用Service.stopSelf()

 

4.生命周期方法

公共方法

IBinderonBind(Intent intent)

除非您为服务提供绑定,否则不需要实现此方法,因为默认实现返回null。

voidonCreate()

首次创建服务时由系统调用。

voidonDestroy()

由系统调用以通知服务它已不再使用且正在被删除。

voidonStart(Intent intent, int startId)

不推荐使用此方法。onStartCommand(Intent, int, int)

intonStartCommand(Intent intent, int flags, int startId)

您不应该为IntentService重写此方法。

voidsetIntentRedelivery(boolean enabled)

设置意向重新传递首选项。

受保护的方法

abstract voidonHandleIntent(Intent intent)

在工作线程上调用此方法并请求处理。

构造函数:public IntentService(String name)

参数
nameString:用于命名工作线程,必填,用于标识工作线程名字

 

gitHub:https://github.com/wkangle

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值