Android Service之串行化Service:IntentService(系列3)

来源:互联网 发布:keeper是什么软件 编辑:程序博客网 时间:2024/04/20 10:02


Android Service之串行化Service:IntentService(系列3)


之前写了一系列的文章关于Android Service。
(文章1)《Android Service简介(系列1)》文章链接地址:http://blog.csdn.net/zhangphil/article/details/49373939 
(文章2)《Android Activity与Service数据交互:Binder、bindService(系列2)》文章链接地址:http://blog.csdn.net/zhangphil/article/details/49385005

文章1简介了如何使用Android Service。文章2简介了出于前台的Android Activity与后台的Service数据交互。
Android最基础的类Service,提供了通常所需的后台任务处理框架。上层处于前台的Activity通过startService启动后台的Service,然后Service进入onStartCommand做耗时任务。这里面潜在有一个问题,假设Activity里面如果不停的startService启动后台的Service,那么将导致onStartCommand不断的反复调用。这意味着,假如代码中这个Service里面的onStartCommand代码块涉及到并发、多线程时候,就要非常小心处理这种多任务情况。可事实上这种多线程任务在Service里面编程设计实现起来比较复杂和琐碎,好在Android体系架构中为了处理这种开发场景,提供了IntentService。
IntentService也是一个Android Service,IntentService在类层次结构上继承自Service。但IntentService与Service有很大不同。
IntentService是串行化处理后台的Service任务的。Service可以在onStartCommand设计复杂的多线程编程模型。然而IntentService已经将此onStartCommand重载,并引入onHandleIntent,开发者不用关心onStartCommand的任务传导给onHandleIntent的内部实现机制。开发者只需要专心在onHandleIntent里面写串行化的任务逻辑代码即可。开发者该怎么简单的理解IntentService呢?可以这么简单的认为IntentService的编程模型:
(1)当上层代码(通常也就是前台的Activity通过startService启动IntentService)启动IntentService后,IntentService将进入onHandleIntent(不用关心onStartCommand是如何维护任务队列和派发任务的),由于Android系统设计时候已经将onHandleIntent线程化处理,所以可以随意的在onHandleIntent里面做大量耗时操作。
(2)要注意,虽然可以通过startService反复启动IntentService,但是在IntentService中,这些任务将排成一个FIFO的任务队列,依次顺序执行,而不是并发同时执行这些任务。可以startService Intent A,B,C,D,E,,,,但只会串行的顺序执行A,B,C,D,E,,,,。只有在前一个任务完成后才会接着顺序执行下一个任务。换一种理解方式,可以认为同时为每个startService运行了多个线程onHandleIntent,但这些线程只会是FIFO顺序执行。这不同于Service,如果是Service,那么将在onStartCommand并发执行。
(3)虽然可以在上层Activity中反复多次执行startService启动IntentService从而进入onHandleIntent执行后台任务,但只需要调用一次stopService,就可以停止所有IntentService队列中的onHandleIntent,但有一点:如果在调用stopService时候,其中有一个onHandleIntent正在处理还尚未完成,那么将此onHandleIntent不受stopService影响直到处理完成。
给出测试代码。
IntentService的类:

[java] view plain copy
  1. package zhangphil.service;  
  2.   
  3. import android.app.IntentService;  
  4. import android.content.Intent;  
  5. import android.util.Log;  
  6.   
  7. public class MyService extends IntentService {  
  8.   
  9.     public MyService() {  
  10.         super("MyAppService");  
  11.     }  
  12.   
  13.     /* 
  14.      * 不要在IntentService里面画蛇添足重载onCreate() 否则将会引起代码崩溃! 
  15.      *  
  16.      * @Override public void onCreate(){ super.onCreate(); } 
  17.      */  
  18.   
  19.     /* 
  20.      * 可以不用关心onStartCommand onStartCommand将会把intent串行派发到onHandleIntent里面顺序线程化执行 
  21.      */  
  22.     @Override  
  23.     public int onStartCommand(Intent intent, int flags, int startId) {  
  24.         Log.d(this.getClass().getName(), "startId:" + startId);  
  25.         return super.onStartCommand(intent, flags, startId);  
  26.     }  
  27.   
  28.     // onHandleIntent已经线程化,可以在这里面做耗时操作。  
  29.     @Override  
  30.     protected void onHandleIntent(Intent intent) {  
  31.         Log.d(this.getClass().getName(), "onHandleIntent");  
  32.   
  33.         try {  
  34.             myLongTimeTask();  
  35.         } catch (InterruptedException e) {  
  36.             e.printStackTrace();  
  37.         }  
  38.     }  
  39.   
  40.     private void myLongTimeTask() throws InterruptedException {  
  41.         for (int i = 0; i < 5; i++) {  
  42.             Log.d(this.getClass().getName(), "i:" + i);  
  43.             Thread.sleep(2000);  
  44.         }  
  45.     }  
  46. }  


测试的Activity MainActivity.Java:

[java] view plain copy
  1. package zhangphil.service;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.widget.Button;  
  8.   
  9.   
  10. public class MainActivity extends Activity {  
  11.   
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.activity_main);  
  16.   
  17.         Button start = (Button) findViewById(R.id.start);  
  18.         start.setOnClickListener(new View.OnClickListener() {  
  19.   
  20.             @Override  
  21.             public void onClick(View v) {  
  22.                 startMyAppService();  
  23.             }  
  24.         });  
  25.   
  26.         Button stop = (Button) findViewById(R.id.stop);  
  27.         stop.setOnClickListener(new View.OnClickListener() {  
  28.   
  29.             @Override  
  30.             public void onClick(View v) {  
  31.                 stopMyAppService();  
  32.             }  
  33.         });  
  34.     }  
  35.   
  36.     private void startMyAppService() {  
  37.         startService(new Intent(this, MyService.class));  
  38.     }  
  39.   
  40.     private void stopMyAppService() {  
  41.         Intent intent = new Intent(this, MyService.class);  
  42.         boolean bool = stopService(intent);  
  43.     }  
  44. }  



MainActivity.java需要的布局文件activity_main.xml文件提供两个按钮,一个用于启动IntentService的start;另外一个是关闭IntentService的stop:

[html] view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"  
  6.     tools:context="zhangphil.service.MainActivity" >  
  7.   
  8.     <Button  
  9.         android:id="@+id/start"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="开始" />  
  13.   
  14.     
  15.     <Button  
  16.         android:id="@+id/stop"  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:text="停止" />  
  20.   
  21. </LinearLayout>  

0
0 0
原创粉丝点击