安卓学习笔记之IntentService浅析
来源:互联网 发布:云墙翻墙软件如何使用 编辑:程序博客网 时间:2024/05/13 19:12
一、原理概述
IntentService继承自Service,内部的ServiceHandler(Handler)与HandlerThread协作来完成异步任务。每当有意图任务时,IntentService的onStartCommand方法会被调用,它内部调用了onStart方法,在onStart方法中向ServiceHandler发消息,ServiceHandler收到消息后会调用onHandleIntent方法来处理后台任务,当任务完成时,IntentService会调用stopSelf(startId)来停止服务。
HandlerThread相关知识可以参考这里安卓学习笔记之HandlerThread
二、特点
1、IntentService是一个抽象类,使用时应该创建子类继承它,并重写onHandleIntent方法,注意,不要试图重写onStartCommand方法
2、IntentService内部ServiceHandler(Handler)与HandlerThread协同合作,在子线程中执行耗时操作
3、因为IntentService它是一个服务类,优先级别比一般的线程高,不容易被系统销毁,所以可以做一些耗时操作,同时它会在任务完成时进行自我停止,不要手动调用sotpSelf方法
4、onBind方法不是必须重写的,只有当你需要绑定服务时,才需要重写。
三、源码分析
1、构造方法,被子类构造调用,用于给内部HandlerThread命名
/** * Creates an IntentService. Invoked by your subclass's constructor. * * @param name Used to name the worker thread, important only for debugging. */ public IntentService(String name) { super(); mName = name; }
2、onCreate方法,创建HandlerThread,并让ServiceHandler与其进行关联
@Override public void onCreate() { // TODO: It would be nice to have an option to hold a partial wakelock // during processing, and to have a static startService(Context, Intent) // method that would launch the service & hand off a wakelock. super.onCreate(); HandlerThread thread = new HandlerThread("IntentService[" + mName + "]"); thread.start(); mServiceLooper = thread.getLooper(); mServiceHandler = new ServiceHandler(mServiceLooper); }
3、onStartCommand,回调onStart方法。mRedelivery是表示是否需要重新接受意图(当onHandleIntent没有执行完而IntentService被销毁时),通过setIntentRedelivery设置。
/** * You should not override this method for your IntentService. Instead, * override {@link #onHandleIntent}, which the system calls when the IntentService * receives a start request. * @see android.app.Service#onStartCommand */ @Override public int onStartCommand(Intent intent, int flags, int startId) { onStart(intent, startId); return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY; }
setIntentRedelivery,设置当进程被重建时是否需要重新接受意图,true表示需要,并且当有多个意图时,只能保证最近的一个可以收到。
/** * Sets intent redelivery preferences. Usually called from the constructor * with your preferred semantics. */ public void setIntentRedelivery(boolean enabled) { mRedelivery = enabled; }
4、onStart方法,向mServiceHandler发送消息
@Override public void onStart(Intent intent, int startId) { Message msg = mServiceHandler.obtainMessage(); msg.arg1 = startId; msg.obj = intent; mServiceHandler.sendMessage(msg); }
5、ServiceHandler如下,在收到消息时会回调onHandleIntent来处理意图
private final class ServiceHandler extends Handler { public ServiceHandler(Looper looper) { super(looper); } @Override public void handleMessage(Message msg) { onHandleIntent((Intent)msg.obj); stopSelf(msg.arg1); }}
6、onHandleIntent,它是一个抽象方法,我们需要重写它并处理意图。它运行在HandlerThread线程中,并在同一时刻只有一个任务执行,其他需排队等待
/** * This method is invoked on the worker thread with a request to process. * Only one Intent is processed at a time, but the processing happens on a * worker thread that runs independently from other application logic. * So, if this code takes a long time, it will hold up other requests to * the same IntentService, but it will not hold up anything else. * When all requests have been handled, the IntentService stops itself, * so you should not call {@link #stopSelf}. * * @param intent The value passed to {@link * android.content.Context#startService(Intent)}. */ protected abstract void onHandleIntent(Intent intent);
四、简单案例
在Mainactivity中向自定义的IntentService发起多次意图请求,在处理每个意图时模拟了耗时操作,观察执行的时间顺序。
自定义Service继承IntentService
package com.yu.threadspool; import android.app.IntentService; import android.content.Intent; import android.os.SystemClock; import android.util.Log; public class IntentServiceTest extends IntentService { public IntentServiceTest() { super("IntentServiceThread"); } // 处理意图 @Override protected void onHandleIntent(Intent intent) { String action = intent.getAction(); if ("com.yu.threadspool.action1".equals(action)) { Log.e("TAG", "处理com.yu.threadspool.action1"); SystemClock.sleep(2000); // 模拟耗时 } if ("com.yu.threadspool.action2".equals(action)) { Log.e("TAG", "处理com.yu.threadspool.action2"); SystemClock.sleep(2000); // 模拟耗时 } if ("com.yu.threadspool.action3".equals(action)) { Log.e("TAG", "处理com.yu.threadspool.action3"); } } @Override public void onDestroy() { super.onDestroy(); Log.e("TAG", "onDestroy"); } }
在MainActivity中启动三个意图
public void intentHandler(View view) { Intent intent=new Intent(this,IntentServiceTest.class); intent.setAction("com.yu.threadspool.action1"); startService(intent); intent.setAction("com.yu.threadspool.action2"); startService(intent); intent.setAction("com.yu.threadspool.action3"); startService(intent); }
执行结果
09-08 21:53:31.331: E/TAG(3235): 处理com.yu.threadspool.action1 09-08 21:53:33.331: E/TAG(3235): 处理com.yu.threadspool.action2 09-08 21:53:35.331: E/TAG(3235): 处理com.yu.threadspool.action3 09-08 21:53:35.331: E/TAG(3235): onDestroy
从执行结果可以看出,三个意图的执行是按顺序执行的.
- 安卓学习笔记之IntentService浅析
- 安卓学习笔记之IntentService
- 安卓学习笔记---IntentService的分析与使用
- 安卓学习笔记之AsyncTask机制浅析
- 【安卓笔记】IntentService源码剖析
- 【安卓笔记】IntentService源码剖析
- 安卓-IntentService使用
- Android之IntentService源码浅析
- 【安卓笔记】GestureDetector浅析
- IntentService学习笔记
- 安卓学习之控件学习笔记
- Android 之IntentService学习
- Android学习之IntentService
- 读IntentService、HandlerThread源码之浅析
- 安卓之路-学习笔记1
- 安卓之路-学习笔记2
- 安卓之路-学习笔记3
- 安卓学习笔记之布局
- 安卓aidl编程基础
- 第二周项目3 体验复杂度(比较两种排序算法的运行时间)
- 在唯一密钥属性“name”设置为“Session”时,无法添加类型为“add”的重复集合项
- Spring hibernate annotation 开发主键生成策略
- iOS 波浪效果
- 安卓学习笔记之IntentService浅析
- groovy maven 配置文件
- Tomcat端口被占用解决
- (移动开发)支付宝支付配置流程
- jquery可拖动的个人相册特效--照片墙
- canvas初体验
- Android 界面适配错误解决方案
- Android Studio 下使用Xstream 详解
- BCGSoft Demo示例展示:网格示例