Android的IntentService、JobIntentService介绍
IntentService是Service的子类,在独立的handler线程里处理异步任务请求。
IntentService中HandlerThread线程类,开启了一个HandlerThread线程实例,这个实例做一件就是开辟一个线程,并创建Looper循环器和消息队列MessageQueue。最后在IntentService里,通过HandlerThread线程实例获得Looper,然后IntentService里的ServiceHandler实例将与此Looper进行绑定。
所以当Clients通过startService(Intent)的方式开启服务并发送任务请求时,会执行IntentService类的onCreate方法,最终在onStartCommand里将任务送入消息队列,尔后,再startService时,都只会执行onStartCommand把任务放入消息队列中,而不会再执行onCreate。
IntentService在工作线程里运行,将依次处理每一个Intent。当执行完所有Intent时,它就会关闭服务。所有的请求都会在一个单例工作线程中被处理,每一时刻只能处理一个请求。这个工作线程不会阻塞应用的主线程中的主循环。
使用时,只需要继承IntentService类,并实现onHandleIntent(Intent)方法。
留意:
在Android 8.0 (API level 26)或以上,IntentService的所有后台执行会受到限制约束。所以在Android 8.0或更高的平台上,最好使用android.support.v4.app.JobIntentService。
使用IntentService的步骤
1、定义继承IntentService类,并实现onHandleIntent(Intent)方法
package com.ti.myintentservice;
import android.app.IntentService;
import android.content.Intent;
import android.content.Context;
import android.util.Log;
public class MyIntentService extends IntentService { public MyIntentService() { super("MyIntentService"); } @Override protected void onHandleIntent(Intent intent) { Log.d("MyIntentService所在线程",Thread.currentThread().getId()+""); if (intent != null) { Log.d("MyIntentService服务#","开始工作了"); } }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
2、在AndroidManifest.xml里注册
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ti.myintentservice">
... <application ...> <service android:name=".MyIntentService" android:exported="false"></service> ... </application>
</manifest>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
3、启动服务与关闭服务
开启:
Intent intent = new Intent(MainActivity.this,MyIntentService.class);
startService(intent);
- 1
- 2
关闭:
Intent intent = new Intent(MainActivity.this,MyIntentService.class);
stopService(intent);
- 1
- 2
在Android 8.0上使用JobIntentService
开发者甚至不需要关心JobIntentService的生命周期,不需要startService()方法,这样也就避免了开可能的crash问题,通过静态方法就可以启动,非常友好。
JobIntentService使用步骤:
1、继承JobIntentService,并重写onHandleWork方法
public class MyJobIntentService extends JobIntentService { @Override protected void onHandleWork(@NonNull Intent intent) { Log.d("MyJobIntentService所在线程", Thread.currentThread().getId() + ""); Log.d("MyJobIntentService服务#", "开始工作了"); }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
2、在AndroidManifest.xml里注册
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ti.myintentservice"> <application ..."> <service android:name=".MyJobIntentService" android:exported="false" android:permission="android.permission.BIND_JOB_SERVICE"><!--android.permission.BIND_JOB_SERVICE这个权限一定要有,否则程序会崩溃--> </service> ... </application>
</manifest>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
3、启动服务
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { int JOB_ID = 8; Intent work = new Intent(); work.putExtra("key","value555555"); MyJobIntentService.enqueueWork(MainActivity.this,MyJobIntentService.class,JOB_ID, work);
}
- 1
- 2
- 3
- 4
- 5
- 6
与传统的Service对比
类型 | 运行线程 | 结束服务操作 |
---|---|---|
IntentService(继承自Service) | 创建一个工作线程处理多线程任务,线程优先级相对普通线程要高 | 不需要手动结束,在任务处理完后,会自动关闭服务 |
JobIntentService(继承自Service,android8.0后推荐用这个) | 创建一个工作线程处理多线程任务,线程优先级相对普通线程要高 | 不需要手动结束,在任务处理完后,会自动关闭服务 |
Service | 在主线程里运行,因此不能处理耗时操作,否则会出现ANR | 需要手动调用stopService方法 |
文章来源: blog.csdn.net,作者:WongKyunban,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/weixin_40763897/article/details/97109321
- 点赞
- 收藏
- 关注作者
评论(0)