更好用的service: IntentService

xiaoxiao2021-02-28  29

IntentService继承自Service类,至于为什么要用IntentService,因为它真的很好用。

Android的Service分两种,Started和Bound,分别是通过startService()和bindService()创建的,两者用途也有所区别,这个在这里就不作详解。IntentService是针对Started Service设计的,由于它默认实现的onBind()方法返回值是null,所以不适合bindService()。

如果直接继承Service,不能把耗时或阻塞的代码写在onStartCommand()等回调方法中,因为这些方法是在主线程中运行的,那样会影响主线程运行,影响用户使用。所以一般会在onStartCommand()启动线程来运行耗时任务,这样任务会在后台执行,不会影响主线程。

IntentService也是同样的原理,只不过大大减少了代码量:

[java]  view plain  copy   public class TestIntentService extends IntentService {          /**       * 需要一个无参数的构造方法,调用父类的带String参数的构造方法,参数是线程名称       */       public TestIntentService() {           super("TestIntentService");       }          /**       * 重写IntentService的onHandleIntent,处理耗时的任务。这个方法是在单独线程中运行,而不是在主线程中,所以不会阻塞主线程。       */       @Override       protected void onHandleIntent(Intent intent) {           // 耗时任务代码       }   }   上面的代码仅仅写了一个构造方法以及重写了onHandleIntent()方法。onHandleIntent()方法不是在主线程中运行,而是在单独线程中运行,所以不会影响主线程。相当于IntentService 已经帮我们启动了一个线程,所以我们不用自己去写启动线程的代码。

官方文档上对IntentService 是这样描述的: 1、IntentService 会创建一个线程,来处理所有传给onStartCommand()的Intent请求。 2、对于startService()请求执行onHandleIntent()中的耗时任务,会生成一个队列,每次只有一个Intent传入onHandleIntent()方法并执行。也就是同一时间只会有一个耗时任务被执行,其他的请求还要在后面排队, onHandleIntent()方法不会多线程并发执行。 3、当所有startService()请求被执行完成后,IntentService 会自动销毁,所以不需要自己写stopSelf()或stopService()来销毁服务。 4、提供默认的onBind()实现 ,即返回null,不适合绑定的 Service。 5、提供默认的 onStartCommand() 实现,将intent传入等待队列中,然后到onHandleIntent()的实现。所以如果需要重写onStartCommand() 方法一定要调用父类的实现。

针对2、3两点,下面进行一些实验:

IntentService中,对于每个Intent请求,在onHandleIntent()中执行一个耗时5秒的任务:

[java]  view plain  copy   public class TestIntentService extends IntentService {          public TestIntentService() {           super("TestIntentService");       }          @Override       protected void onHandleIntent(Intent intent) {           DateFormat format = DateFormat.getTimeInstance();           Log.v("test""onHandleIntent开始:" + format.format(new Date()));                      // 这里Thread.sleep(5000)模拟执行一个耗时5秒的任务           try {               Thread.sleep(5000);           } catch (InterruptedException e) {           }                      Log.v("test""onHandleIntent完成:" + format.format(new Date()));       }              // 这里还重写了onDestroy,记录日志用于观察Service何时销毁       @Override       public void onDestroy() {           Log.v("test""onDestroy");           super.onDestroy();       }   }   在Activity中,添加一个按钮,通过startService()来启动Service:

[html]  view plain  copy   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"       android:layout_width="match_parent"       android:layout_height="match_parent">          <Button            android:id="@+id/button"           android:layout_width="match_parent"           android:layout_height="wrap_content"           android:text="启动IntentService"/>      </LinearLayout>   [java]  view plain  copy   public class MainActivity extends Activity {          @Override       protected void onCreate(Bundle savedInstanceState) {           super.onCreate(savedInstanceState);           setContentView(R.layout.activity_main);           Button button = (Button) findViewById(R.id.button);           button.setOnClickListener(new OnClickListener() {                              @Override               public void onClick(View view) {                   startService(new Intent(MainActivity.this, TestIntentService.class));               }           });        }   }  

运行程序,只点击一次按钮, Log:

onHandleIntent开始:5:38:01 AM onHandleIntent完成:5:38:06 AM onDestroy

连续点击3次按钮,Log:

onHandleIntent开始:5:42:14 AM onHandleIntent完成:5:42:19 AM onHandleIntent开始:5:42:19 AM onHandleIntent完成:5:42:24 AM onHandleIntent开始:5:42:24 AM onHandleIntent完成:5:42:29 AM onDestroy

上面的结果证实了第2、3两条: 多次startService请求执行耗时任务,不会并发执行onHandleIntent()方法,而是一个一个顺序执行。当所有的任务执行完成,IntentService会自动销毁。

作者:叉叉哥   转载请注明出处:http://blog.csdn.net/xiao__gui/article/details/11579087

转载请注明原文地址: https://www.6miu.com/read-750182.html

最新回复(0)