Android开发:IntentService使用(源码分析)
2015-06-25 16:38
302 查看
IntentService继承自Service,那么它与Service又有哪些不同?使用起来有哪些需要注意的地方呢?什么时候使用Service,什么时候使用IntentService?一起来看看。
转载请注明作者xiong_it和本文链接:/article/2590643.html,谢谢!
(1)IntentService直接创建一个默认的工作线程(子线程)无需担心线程阻塞问题发生,Service默认在主线程(UI线程)执行;
(2)IntentService直接创建一个工作队列,将意图传递给你onHandleIntent()的实现,并且一次只处理一个意图,Service默认将意图传递给onStartCommand()处理;
(3)IntentService当请求完成后自己会调用stopSelf(),所以你就不用调用该方法了,Service需要手动调用stopSelf来终止服务(前提是使用startService()开启服务)。
下面,我们将通过源码来认识IntentService。
IntentService的OnStartCommand()源码
在IntentService中,当请求完成后,mServiceHandler会收到消息,它会自动调用stopSelf()终止自身服务:
每个类的构造函数都是必需的,在我们继承IntentService时,会提示实现一个构造函数,使用Eclipse自动生成的构造函数是这样的:
这就很奇怪了:IntentService在没有空的构造方法运行崩溃,加上又警告提示,IntentService中没有定义这样的构造方法。解决办法是:
转载请注明作者xiong_it和本文链接:/article/2590643.html,谢谢!
转载请注明作者xiong_it和本文链接:/article/2590643.html,谢谢!
IntentService与Service的区别
使用intentService与service有什么不同呢:(1)IntentService直接创建一个默认的工作线程(子线程)无需担心线程阻塞问题发生,Service默认在主线程(UI线程)执行;
(2)IntentService直接创建一个工作队列,将意图传递给你onHandleIntent()的实现,并且一次只处理一个意图,Service默认将意图传递给onStartCommand()处理;
(3)IntentService当请求完成后自己会调用stopSelf(),所以你就不用调用该方法了,Service需要手动调用stopSelf来终止服务(前提是使用startService()开启服务)。
下面,我们将通过源码来认识IntentService。
IntentService源码分析
@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); }从IntentService的onCreate()可以看出,在IntentService内部,给我们创建了一个HandlerThread的工作线程,并且得到一个looper对象,用于消息队列传输,并且由mServiceHandler处理相应消息。正好验证了IntentService与Service的区别的(1)(2)两点。有的朋友可能会问了,Looper一般情况下,只能在主线程使用吗?是的,一般情况下,Looper只在主线程中使用,但是HandlerThread继承自Thread,重写了Thread的run()方法:
@Override public void run() { mTid = Process.myTid(); Looper.prepare(); synchronized (this) { mLooper = Looper.myLooper(); notifyAll(); } Process.setThreadPriority(mPriority); onLooperPrepared(); Looper.loop(); mTid = -1; }在子线程中使用Looper时,需要使用Looper.prepare(),和Looper.loop()来使得当前线程(也就是子线程)作为Looper的使用场所,并传递消息队列。
IntentService的OnStartCommand()源码
/** * 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; }指出:在IntentService中,无需实现onStartCommand(),我们需要做的就是实现onHandlerIntent()方法(You should not override this method for your IntentService. Instead override {@link #onHandleIntent}, which the system calls when the IntentService)。
在IntentService中,当请求完成后,mServiceHandler会收到消息,它会自动调用stopSelf()终止自身服务:
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); } }这也验证了上面的区别(3),而Service是做不到这一点的,我们需要手动调用stopSelf来自杀。
IntentService的使用注意事项
IntentService构造方法的问题每个类的构造函数都是必需的,在我们继承IntentService时,会提示实现一个构造函数,使用Eclipse自动生成的构造函数是这样的:
/** * @param name */ public MyService(String name) { super(name); // TODO Auto-generated constructor stub }但是运行的时候,开启该服务会崩溃,并报出错误:
java.lang.InstantiationException: can't instantiate class : no empty constructor意思是说,需要一个空的构造方法,于是,给MyService加上一个无参数的空构造方法:
public MyService() { }但是Eclipse红色警告:
Implicit super constructor IntentService() is undefined. Must explicitly invoke another constructor
这就很奇怪了:IntentService在没有空的构造方法运行崩溃,加上又警告提示,IntentService中没有定义这样的构造方法。解决办法是:
public MyService() { super(SERVICE_NAME);//SERVICE_NAME是自定义的字符串 }
何时使用IntentService
相信看完,大家应该对什么时候使用IntentService有数了,只要了解了IntentService和Service的区别,还是很好把我的,希望本文对大家有所帮助。转载请注明作者xiong_it和本文链接:/article/2590643.html,谢谢!
相关文章推荐
- (转)Android开发--常用的传感器总结
- android缓存数据到本地放在哪儿最好?
- android notification完全解析
- android学习之---屏幕知识
- Android签名机制:生成keystore、签名、查看签名信息
- Android实现心跳的效果
- 关于webservice的问题
- android电池系统
- Android应用程序签名详解 简介
- 最新Android SDK_API_开发包_离线包_下载 指导
- 分享:Android 应用有哪些常见,浅谈常被利用的安全漏洞?
- Android基础和进阶知识总结
- Android 图片压缩
- android:descendantFocusability用法简析
- 如何为你的 Android 应用添加 Google Analytics
- Android 安全——Java环境动态加载Jar之Jar包的加密和解密
- Android fragment生命周期
- Android使用sharesdk一键分享
- android style 退出动画 解决退出动画无效问题
- android file.createnewfile ioexception