android 中IntentService的使用场景

生也有涯,知也无涯。这篇文章主要讲述android 中IntentService的使用场景相关的知识,希望能为你提供帮助。
IntentService是继承并处理异步请求的一个类,在IntentService内有一个工作线程来处理耗时操作,启动IntentService的方式和启动传统的Service一样,同时,当任务执行完后,IntentService会自动停止,而不需要我们手动去控制或stopSelf()。另外,可以启动IntentService多次,而每一个耗时操作会以工作队列的方式在IntentService的onHandleIntent回调方法中执行,并且,每次只会执行一个工作线程,执行完第一个再执行第二个,以此类推。
在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
【android 中IntentService的使用场景】
简单说,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!

package im.weiyuan.com.viewutils; import android.app.IntentService; import android.content.Intent; import android.content.Context; import android.os.SystemClock; import android.util.Log; /** * An {@link IntentService} subclass for handling asynchronous task requests in * a service on a separate handler thread. * < p> * TODO: Customize class - update intent actions, extra parameters and static * helper methods. */ public class MyIntentService extends IntentService {/** * 必须要写一个无参数的构造函数,然后调用父类的 super("MyIntentService"); * 其中MyIntentService就是执行onHandleIntent对应的线程的名字 * */ public MyIntentService() { super("MyIntentService"); }/** * onHandleIntent函数是在子线程中去执行处理的,所以这里就没有必要去开启线程 * */@Override protected void onHandleIntent(Intent intent) { Log.d("123456","onHandleIntent is called"); /** * 模拟耗时操作 * */ SystemClock.sleep(10000); Log.d("123456","onHandleIntent is out"); }/** * onHandleIntent函数中的耗时任务执行完成后,服务会自动销毁 * 调用onDestroy函数 * * */@Override public void onDestroy() { super.onDestroy(); Log.d("123456","onDestroy is called"); } }

< ?xml version="1.0" encoding="utf-8"?> < manifest xmlns:android="http://schemas.android.com/apk/res/android" package="im.weiyuan.com.viewutils"> < permission android:name="com.weiyuan.sb" /> < uses-permission android:name="com.weiyuan.sb" /> < application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> < activity android:name=".MainActivity"> < intent-filter> < action android:name="android.intent.action.MAIN" /> < category android:name="android.intent.category.LAUNCHER" /> < /intent-filter> < /activity> < service android:name=".MyIntentService" android:exported="false"> < /service> < /application> < /manifest>

Intent intent = new Intent(MainActivity.this,MyIntentService.class);
startService(intent);
我们来看看日志打印的输出:

07-23 11:52:18.507 14993-15238/im.weiyuan.com.viewutils D/123456: onHandleIntent is called
07-23 11:52:28.508 14993-15238/im.weiyuan.com.viewutils D/123456: onHandleIntent is out
07-23 11:52:28.510 14993-14993/im.weiyuan.com.viewutils D/123456: onDestroy is called
 
不清楚的看博客:
http://blog.csdn.net/ryantang03/article/details/8146154/
相当的经典
 











    推荐阅读