Android IntentService vs Service

Android IntentService vs Service

众所周知,Android中的Service是用于后台服务的,当应用程序被挂到后台的时候,为了保证应用中某些功能仍然可以工作而引入了Service,比如播放音乐。针对service,官方文档有2点重要说明:

1. A Service is not a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of.除非特别说明,否则和应用是在同一个进程中

2. A Service is not a thread. It is not a means itself to do work off of the main thread (to avoid Application Not Responding errors). Service不是一个线程,也就是说service和UI主线程是在同一线程中运行的,因此service不能执行耗时长的处理,防止出现ANR。

因此如果APP需要在service执行长时间处理时,需要起个独立线程来处理该操作。

Android为我们考虑到了这一点,所以提供了IntentService类。IntentService继承于Service,可以认为是开了一个线程来处理事件的service.

IntentService有如下优点:

1. IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work. 队列任务按序执行,全部任务结束后会结束自己。

2. This “work queue processor” pattern is commonly used to offload tasks from an application’s main thread. The IntentService class exists to simplify this pattern and take care of the mechanics. To use it, extend IntentService and implement onHandleIntent(Intent). IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate.

3. All requests are handled on a single worker thread – they may take as long as necessary (and will not block the application’s main loop), but only one request will be processed at a time.所有请求在一个工作线程中完成,但是一次之后处理一个。

下面我们对IntentService代码进行分析:

继承自service

public abstract class IntentService extends Service

有一个工作线程,在onCreate中创建该线程

HandlerThread thread = new HandlerThread(“IntentService[” + mName + “]”);

内部通过handler来处理startService发送的消息事件。同样在onCreate函数中创建thread looper和Handler

mServiceLooper = thread.getLooper();

mServiceHandler = new ServiceHandler(mServiceLooper);

而在每次调用startService时会将任务添加到队列中

public void onStart(Intent intent, int startId) {
        Message msg = mServiceHandler.obtainMessage();
        msg.arg1 = startId;
        msg.obj = intent;
        mServiceHandler.sendMessage(msg);
}

Handler执行过程为:

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);
        }
}

从上面可见IntentService是按照顺序来执行的,全部任务执行完成后工作线程finish。

使用如下:

在manifext.xml中添加声明

然后在调用的地方直接使用:

public void startService() {
  Intent intent = new Intent(this, XXXIntentService.class);
  startService(intent);
 }

 public void stopService() {
  Intent intent = new Intent(this, XXXIntentService.class);
  stopService(intent);
 }

其中调用stopService会直接调用IntentService的onDestroy函数,当前正在执行的任务不会暂停,等该任务完成后线程关闭。如果需要立即结束任务,则可以通过设置变量来让当前任务尽快结束。

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-10 19:16:48

Android IntentService vs Service的相关文章

Android IntentService完全解析 当Service遇到Handler

一 概述 大家都清楚,在Android的开发中,凡是遇到耗时的操作尽可能的会交给Service去做,比如我们上传多张图,上传的过程用户可能将应用置于后台,然后干别的去了,我们的Activity就很可能会被杀死,所以可以考虑将上传操作交给Service去做,如果担心Service被杀,还能通过设置startForeground(int, Notification)方法提升其优先级. 那么,在Service里面我们肯定不能直接进行耗时操作,一般都需要去开启子线程去做一些事情,自己去管理Service

android开发步步为营之60:IntentService与Service的区别

这个面试的时候,相信是面试官最爱的问题之一.简单的来说,IntentService继承至Service,Service和Acitivity一样是依附于应用主进程的,它本身不是一个进程或者一个线程.一些耗时的操作可能会引起ANR的bug,(本文测试的时候,Service执行20秒没有报ANR),而IntentService,看它的源代码,onCreate()其实是创建了一个新的线程. /* * Copyright (C) 2008 The Android Open Source Project *

Android IntentService全然解析 当Service遇到Handler

转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/47143563: 本文出自:[张鸿洋的博客] 一 概述 大家都清楚.在Android的开发中,凡是遇到耗时的操作尽可能的会交给Service去做.比方我们上传多张图,上传的过程用户可能将应用置于后台.然后干别的去了,我们的Activity就非常可能会被杀死,所以能够考虑将上传操作交给Service去做,假设操心Service被杀,还能通过设置startForeground(in

[Android 基础系列]Service、IntentService和习以为常的误解

前言: 也许是低门槛原因,最初接触Android的人写了很多书.博文,创造了一个邪论:Activity就是弄界面的,Service就是弄后台的,进而将"播放音乐"这种演变为"耗时操作",进而演绎成:"耗时的.长时间运行的都需要使用service".只想说:MDZZ! 原意是想全文自己写,但看了一眼API文档,整理的实在是太好了,所以本文会摘录API的内容并结合重点写一点内容. 正文: Service: API文档中的概述如下: A Service

Android:远程服务Service(含AIDL & IPC讲解)

前言 Service作为Android四大组件之一,应用非常广泛 本文将介绍Service其中一种常见用法:远程Service 如果你对Service还未了解,建议先阅读我写的另外一篇文章: Android四大组件:Service史上最全面解析 目录 1. 远程服务与本地服务的区别 远程服务与本地服务最大的区别是:远程Service与调用者不在同一个进程里(即远程Service是运行在另外一个进程):而本地服务则是与调用者运行在同一个进程里 二者区别的详细区别如下图: 2. 使用场景 多个应用程

android笔记:Service

服务:在后台运行,没有界面的组件. 服务生命周期: startService(): onCreate()-->onStartCommand()-->onDestroy().bindService():  onCreate()-->onBind()-->onUnbind()-->onDestroy(). 一.定义一个服务,得做到以下: 1.继承Service,重写onBind().onCreate().onStartCommand()和 onDestroy(); 2.在Andr

Android IntentService 源码分析

IntentService简介: IntentService是一个通过Context.startService(Intent)启动可以处理异步请求的Service,使用时你只需要继承IntentService和重写其中的onHandleIntent(Intent)方法接收一个Intent对象,该服务会在异步任务完成时自动停止服务. 所有的请求的处理都在IntentService内部工作线程中完成,它们会顺序执行任务(但不会阻塞主线程的执行),某一时刻只能执行一个异步请求. IntnetServi

Android 服务类Service 的详细学习

上一篇说到了通知栏Notification,提起通知栏,不得让人想到Service以及BroadcastReceive,作为android的4大组建的2个重要成员,我们没少和它们打交道.它们可以在无形中使我们的软件和网络.数据库.系统等进行交互,之后通过UI(Notification就是一种展示方式)把结果展现在我们面前.可以说,他们是android生命体系里面的神经系统,通过反射条件让身体展现不同的状态.在整个系统中,广播接收器充当着是传输者和监听者的角色,它把系统的一点点变化都反馈上去,之后

【转载】Android IntentService使用全面介绍及源码解析

一 IntentService介绍 IntentService定义的三个基本点:是什么?怎么用?如何work? 官方解释如下: //IntentService定义的三个基本点:是什么?怎么用?如何work?*/ 1.IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. 2.Clients send requests through