背景/简介
-
在Android开发中,凡是遇到耗时操作通常都会交给Service去做,比如上传文件,下载文件等需要耗时的任务。出于对内存的考虑,如果担心Service被杀,通常还能通过
startForeground(int, Notification)将服务升级为前台服务,提升优先级。 -
Service默认是执行在主线程上的,所以,在Service里面不能直接做耗时操作,否者回造成ANR异常,所以通常需要在服务启动后,开启子线程去做一些事情,自己去管理Service的生命周期以及子线程并非是个优雅的做法,所以Android又提供了
IntentService。
- IntentService是一个基于Service的一个类,用来处理异步的请求。你可以通过startService(Intent)来提交请求,该Service会在需要的时候创建,当完成所有的任务以后自己关闭,且请求是在子线程处理的。
- 继承关系
java.lang.Object ↳ android.content.Context ↳ android.content.ContextWrapper ↳ android.app.Service ↳ android.app.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.生命周期方法
公共方法 | |
|---|---|
IBinder | onBind(Intent intent) 除非您为服务提供绑定,否则不需要实现此方法,因为默认实现返回null。 |
void | onCreate() 首次创建服务时由系统调用。 |
void | onDestroy() 由系统调用以通知服务它已不再使用且正在被删除。 |
void | onStart(Intent intent, int startId) 不推荐使用此方法。 |
int | onStartCommand(Intent intent, int flags, int startId) 您不应该为IntentService重写此方法。 |
void | setIntentRedelivery(boolean enabled) 设置意向重新传递首选项。 |
受保护的方法 | |
|---|---|
abstract void | onHandleIntent(Intent intent) 在工作线程上调用此方法并请求处理。 |
构造函数:public IntentService(String name)
| 参数 | |
|---|---|
name | String:用于命名工作线程,必填,用于标识工作线程名字 |
gitHub:https://github.com/wkangle
本文深入解析Android中的IntentService,一种用于处理异步请求的服务组件。详细介绍了其使用方法、生命周期及注意事项,帮助开发者更好地理解和运用IntentService。
806

被折叠的 条评论
为什么被折叠?



