深入剖析Android系统中Service和IntentService的区别

深入剖析Android系统中Service和IntentService的区别,第1张

概述Android中的Service是用于后台服务的,当应用程序被挂到后台的时候,问了保证应用某些组件仍然可以工作而引入了Service这个概念,那么这里面要强调的是Service不是独立的进程,也不是独立的线程,它是依赖于应用程序

AndroID中的Service是用于后台服务的,当应用程序被挂到后台的时候,问了保证应用某些组件仍然可以工作而引入了Service这个概念,那么这里面要强调的是Service不是独立的进程,也不是独立的线程,它是依赖于应用程序的主线程的,也就是说,在更多时候不建议在Service中编写耗时的逻辑和 *** 作,否则会引起ANR。

那么我们当我们编写的耗时逻辑,不得不被service来管理的时候,就需要引入IntentService,IntentService是继承Service的,那么它包含了Service的全部特性,当然也包含service的生命周期,那么与service不同的是,IntentService在执行onCreate *** 作的时候,内部开了一个线程,去你执行你的耗时 *** 作。

service本身存在两个问题:

(1)service不会专门启动一条单独的进程,service与它所在的应用位于同一个进程。

(2)service也不是专门新的一条线程,不应该在service中处理耗时的 *** 作。

IntentService很好的弥补了这一点:

(1)IntentService会创建单独的worker线程来处理所有的intent请求。

(2)IntentService会创建单独的worker线程来处理onHandleIntent()方法实现的代码。

(3)当所有的请求处理完之后,IntentService会自动停止。

(4)为Service的OnBind()方法提供了默认的实现,返回null。

(5)为service的onStartCommand()方法提供了默认的实现,该实现会将请求intent添加到队列中。

所以对IntentService的使用就是:继承IntentService,重写onHandleIntent()方法即可。

tips:
(1)Intentservice也必须在manifest中声明。
(2)实现类的构造方法必须实现默认的构造方法。

这里我 需要解释以下几个方法,也许大家都已经很清楚了,不过为了抛砖引玉,我还是要提一嘴。

Service中提供了一个方法:

public int onStartCommand(Intent intent,int flags,int startID) {    onStart(intent,startID);    return mStartCompatibility ? START_STICKY_COMPATIBIliTY : START_STICKY;  } 

这个方法的具体含义是,当你的需要这个service启动的时候,或者调用这个servcIE的时候,那么这个方法首先是要被回调的。

同时IntentService中提供了这么一个方法:

protected abstract voID onHandleIntent(Intent intent); 

这是一个抽象方法,也就是说具体的实现需要被延伸到子类。

子类的声明:

public class ChargeService extends IntentService  

上面提到过IntentService是继承Service的,那么这个子类也肯定继承service,那么onHandleIntent()方法是什么时候被调用的呢?让我们具体看IntentService的内部实现:

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);   } }  /**  * 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; }  /**  * Sets intent redelivery preferences. Usually called from the constructor  * with your preferred semantics.  *  * <p>If enabled is true,* {@link #onStartCommand(Intent,int,int)} will return  * {@link Service#START_REDEliVER_INTENT},so if this process dIEs before  * {@link #onHandleIntent(Intent)} returns,the process will be restarted  * and the intent redelivered. If multiple Intents have been sent,only  * the most recent one is guaranteed to be redelivered.  *  * <p>If enabled is false (the default),int)} will return  * {@link Service#START_NOT_STICKY},and if the process dIEs,the Intent  * dIEs along with it.  */ public voID setIntentRedelivery(boolean enabled) {   mRedelivery = enabled; }  @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); }  @OverrIDe public voID onStart(Intent intent,int startID) {   Message msg = mServiceHandler.obtainMessage();   msg.arg1 = startID;   msg.obj = intent;   mServiceHandler.sendMessage(msg); } 

在这里我们可以清楚的看到其实IntentService在执行onCreate的方法的时候,其实开了一个线程HandlerThread,并获得了当前线程队列管理的looper,并且在onStart的时候,把消息置入了消息队列,

@OverrIDe     public voID handleMessage(Message msg) {       onHandleIntent((Intent)msg.obj);       stopSelf(msg.arg1);     } 

在消息被handler接受并且回调的时候,执行了onHandlerIntent方法,该方法的实现是子类去做的。

结论:

IntentService是通过Handler looper message的方式实现了一个多线程的 *** 作,同时耗时 *** 作也可以被这个线程管理和执行,同时不会产生ANR的情况。

总结

以上是内存溢出为你收集整理的深入剖析Android系统中Service和IntentService的区别全部内容,希望文章能够帮你解决深入剖析Android系统中Service和IntentService的区别所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1141276.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存