`

Android中IntentService的原理及使用

 
阅读更多

在Android开发中,我们或许会碰到这么一种业务需求,一项任务分成几个子任务,子任务按顺序先后执行,子任务全部执行完后,这项任务才算成功。那么,利用几个子线程顺序执行是可以达到这个目的的,但是每个线程必须去手动控制,而且得在一个子线程执行完后,再开启另一个子线程。或者,全部放到一个线程中让其顺序执行。这样都可以做到,但是,如果这是一个后台任务,就得放到Service里面,由于Service和Activity是同级的,所以,要执行耗时任务,就得在Service里面开子线程来执行。那么,有没有一种简单的方法来处理这个过程呢,答案就是IntentService。

 

什么是IntentService,首先看看官方的解释:

 

IntentService is a base class forServices that handle asynchronous requests (expressed asIntents) on demand. Clients send requests throughstartService(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

 

 

简单说,IntentService是继承于Service并处理异步请求的一个类,在IntentService内有一个工作线程来处理耗时操作,启动IntentService的方式和启动传统Service一样,同时,当任务执行完后,IntentService会自动停止,而不需要我们去手动控制。另外,可以启动IntentService多次,而每一个耗时操作会以工作队列的方式在IntentService的onHandleIntent回调方法中执行,并且,每次只会执行一个工作线程,执行完第一个再执行第二个,以此类推。

还有一个说明是:

 

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.

大致意思是:所有请求都在一个单线程中,不会阻塞应用程序的主线程(UI Thread),同一时间只处理一个请求。

那么,用IntentService有什么好处呢?首先,我们省去了在Service中手动开线程的麻烦,第二,当操作完成时,我们不用手动停止Service,第三,it's so easy to use!

 

 

ok,接下来让我们来看看如何使用,我写了一个Demo来模拟两个耗时操作,Operation1与Operation2,先执行1,2必须等1执行完才能执行:

新建工程,新建一个继承IntentService的类,我这里是IntentServiceDemo.java

 

[java] view plaincopy
 
  1. public class IntentServiceDemo extends IntentService {  
  2.   
  3.     public IntentServiceDemo() {  
  4.         //必须实现父类的构造方法  
  5.         super("IntentServiceDemo");  
  6.     }  
  7.       
  8.     @Override  
  9.     public IBinder onBind(Intent intent) {  
  10.         System.out.println("onBind");  
  11.         return super.onBind(intent);  
  12.     }  
  13.   
  14.   
  15.     @Override  
  16.     public void onCreate() {  
  17.         System.out.println("onCreate");  
  18.         super.onCreate();  
  19.     }  
  20.   
  21.     @Override  
  22.     public void onStart(Intent intent, int startId) {  
  23.         System.out.println("onStart");  
  24.         super.onStart(intent, startId);  
  25.     }  
  26.   
  27.   
  28.     @Override  
  29.     public int onStartCommand(Intent intent, int flags, int startId) {  
  30.         System.out.println("onStartCommand");  
  31.         return super.onStartCommand(intent, flags, startId);  
  32.     }  
  33.   
  34.   
  35.     @Override  
  36.     public void setIntentRedelivery(boolean enabled) {  
  37.         super.setIntentRedelivery(enabled);  
  38.         System.out.println("setIntentRedelivery");  
  39.     }  
  40.   
  41.     @Override  
  42.     protected void onHandleIntent(Intent intent) {  
  43.         //Intent是从Activity发过来的,携带识别参数,根据参数不同执行不同的任务  
  44.         String action = intent.getExtras().getString("param");  
  45.         if (action.equals("oper1")) {  
  46.             System.out.println("Operation1");  
  47.         }else if (action.equals("oper2")) {  
  48.             System.out.println("Operation2");  
  49.         }  
  50.           
  51.         try {  
  52.             Thread.sleep(2000);  
  53.         } catch (InterruptedException e) {  
  54.             e.printStackTrace();  
  55.         }  
  56.     }  
  57.   
  58.     @Override  
  59.     public void onDestroy() {  
  60.         System.out.println("onDestroy");  
  61.         super.onDestroy();  
  62.     }  
  63.   
  64. }  


我把生命周期方法全打印出来了,待会我们来看看它执行的过程是怎样的。接下来是Activity,在Activity中来启动IntentService:

 

 

[java] view plaincopy
 
  1. public class TestActivity extends Activity {  
  2.     /** Called when the activity is first created. */  
  3.     @Override  
  4.     public void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.main);  
  7.           
  8.         //可以启动多次,每启动一次,就会新建一个work thread,但IntentService的实例始终只有一个  
  9.         //Operation 1  
  10.         Intent startServiceIntent = new Intent("com.test.intentservice");  
  11.         Bundle bundle = new Bundle();  
  12.         bundle.putString("param""oper1");  
  13.         startServiceIntent.putExtras(bundle);  
  14.         startService(startServiceIntent);  
  15.           
  16.         //Operation 2  
  17.         Intent startServiceIntent2 = new Intent("com.test.intentservice");  
  18.         Bundle bundle2 = new Bundle();  
  19.         bundle2.putString("param""oper2");  
  20.         startServiceIntent2.putExtras(bundle2);  
  21.         startService(startServiceIntent2);  
  22.     }  
  23. }  


最后,别忘了配置Service,因为它继承于Service,所以,它还是一个Service,一定要配置,否则是不起作用的,开始我就是忘了,结果半天没反应。

 

 

[html] view plaincopy
 
  1. <service android:name=".IntentServiceDemo">  
  2.             <intent-filter >  
  3.                 <action android:name="com.test.intentservice"/>  
  4.             </intent-filter>  
  5.         </service>  


ok,最后来看看执行结果:

 


从结果可以看到,onCreate方法只执行了一次,而onStartCommand和onStart方法执行了两次,开启了两个Work Thread,这就证实了之前所说的,启动多次,但IntentService的实例只有一个,这跟传统的Service是一样的。Operation1也是先于Operation2打印,并且我让两个操作间停顿了2s,最后是onDestroy销毁了IntentService。

 

这就是IntentService,一个方便我们处理业务流程的类,它是一个Service,但是比Service更智能

分享到:
评论

相关推荐

    android IntentService实现原理及内部代码分享

    android IntentService实现原理及内部代码分享,需要的朋友可以参考一下

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

    主要介绍了Android系统中Service和IntentService的区别,与普通的服务相比,IntentService可以开启单独的线程来处理intent请求,需要的朋友可以参考下

    IntentService使用Demo

    IntentService使用示例,原理以及用途,详细说明了IntentService运行流程

    Android开发艺术探索.任玉刚(带详细书签).pdf

    2.2 Android中的多进程模式 36 2.2.1 开启多进程模式 36 2.2.2 多进程模式的运行机制 39 2.3 IPC基础概念介绍 42 2.3.1 Serializable接口 42 2.3.2 Parcelable接口 45 2.3.3 Binder 47 2.4 Android中的IPC...

    Android知识点及重要代码合集 word文档

    14.5 简单游标适配器的使用及分页效果的实现 207 15.1对手机通讯录的增删改查 211 15.2查询手机通话记录 221 15.3操作手机短信 224 16.1 自定义内容提供者的编写步骤 226 16.2 如何使当前应用的内容提供者可以被其他...

    精通ANDROID 3(中文版)1/2

    23.5.1 在Android搜索中使用操作键  23.5.2 使用应用程序特定的搜索上下文  23.6 资源  23.7 对平板电脑的意义  23.8 小结  第24章 文本到语音转换  24.1 Android中的文本到语音转换  24.2 使用语段...

    Android开发艺术探索

    而对于高级开发者来说,仍然可以从《Android开发艺术探索》的知识体系中获益。 全书目录 ------------------------------------------------------------------- 第1章 Activity的生命周期和启动模式 / 1  1.1 ...

    精通Android 3 (中文版)2/2

    23.5.1 在Android搜索中使用操作键  23.5.2 使用应用程序特定的搜索上下文  23.6 资源  23.7 对平板电脑的意义  23.8 小结  第24章 文本到语音转换  24.1 Android中的文本到语音转换  24.2 使用语段...

    Android锁屏无法继续定位问题

    public class MyIntentService extends IntentService { public MyIntentService() { super("MyIntentService"); } @Override public void onCreate() { super.onCreate(); } @Override protected void ...

    IntentServiceDownloadDemo

    IntentService下载任务使用示例,原理以及用途,详细说明了IntentService运行流程

    hrl_android_notes:整理下碰到的一些面试题和基础知识

    进程被Kill后,保存的数据怎么恢复的2.Service1.service 的生命周期,两种启动方式的区别2.Service启动流程3.Service与Activity怎么实现通信4.IntentService是什么,IntentService原理,应用场景及其与Service的区别5....

    百度地图开发java源码-blog-backup:学习文章,也是我博客的备份

    百度地图开发java源码 学习时候,所做的一些笔记。方便之后复习查阅。...的使用方法和工作原理 本篇介绍 Android 中的线程池 ThreadPoolExecutor 相关概念。 本篇介绍 HandlerThread 和 IntentService 相

    java反编译泄露源码-zhongwenjun.github.com:中文君.github.com

    AsyncTask/HandlerThread/IntentService UI View绘制原则 事件派发机制 /RecyclerView缓存机制 动画机制 开源库 网络:Okhttp/Retrofit/Vollery 图片加载:Fresco/Glide/Picasso/ImageLoader 事件总线:EventBus ...

Global site tag (gtag.js) - Google Analytics