【成都安卓培训】android中intentservice

上传人:wm****3 文档编号:42914273 上传时间:2018-06-04 格式:DOC 页数:5 大小:42.78KB
返回 下载 相关 举报
【成都安卓培训】android中intentservice_第1页
第1页 / 共5页
【成都安卓培训】android中intentservice_第2页
第2页 / 共5页
【成都安卓培训】android中intentservice_第3页
第3页 / 共5页
【成都安卓培训】android中intentservice_第4页
第4页 / 共5页
【成都安卓培训】android中intentservice_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《【成都安卓培训】android中intentservice》由会员分享,可在线阅读,更多相关《【成都安卓培训】android中intentservice(5页珍藏版)》请在金锄头文库上搜索。

1、http:/【成都安卓培训成都安卓培训】Android 中中 IntentService 的原理及使用的原理及使用在 Android 开发中,我们或许会碰到这么一种业务需求,一项任务分成几个子任务,子任务按 顺序先后执行,子任务全部执行完后,这项任务才算成功。那么,利用几个子线程顺序执行是可以达到这个目的的,但是每个线程必须去手动控制,而且得在一个子线程执行完后,再开启另一个子线程。或者,全部放到一个线程中让其顺序执行。这样都可以做到,但是,如果这是一个后台任务,就得放到 Service 里面,由于 Service 和 Activity 是同级的,所以,要执行耗时任务,就得在 Service

2、里面开子线程来执行。那么,有没有一种简单的方法来处理这个过程呢,答案就是 IntentService。http:/什么是 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 In

3、tent in turn using a worker thread, and stops itself when it runs out of work简单说,IntentService 是继承于 Service 并处理异步请求的一个类,在 IntentService 内有一个工作线程来处理耗时操作,启动IntentService 的方式和启动传统 Service 一样,同时,当任务执行完后,IntentService 会自动停止,而不需要我们去手动控制。另外,可以启动 IntentService 多次,而每一个耗时操作会以工作队列的方式在 IntentService 的 onHandleI

4、ntent 回调方法中执行,并且,每次只会执行一个工作线程,执行完第一个再执行第二个,以此类推。 还有一个说明是:http:/All requests are handled on a single worker thread - they may take as long as necessary (and will not block the applications main loop), but only one request will be processed at a time.大致意思是:所有请求都在一个单线程中,不会阻塞应用程序的主线程(UI Thread),同一时间只处理一

5、个请求。 那么,用 IntentService 有什么好处呢?首先,我们省去了在 Service 中手动开线程的麻烦,第二,当操作完成时,我们不用手动停止 Service,第三,its so easy to use!ok,接下来让我们来看看如何使用,我写了一个 Demo 来模拟两个耗时操作,Operation1 与Operation2,先执行 1,2 必须等 1 执行完才能执行:新建工程,新建一个继承 IntentService 的类,我这里是 IntentServiceDemo.java 【成都 安卓培训】java view plaincopyprint?1.public class Int

6、entServiceDemo 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.pr

7、intln(“onCreate“); 18.super.onCreate(); 19. 20.21. Override http:/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

8、(“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 int

9、ent) 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. cat

10、ch (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. http:/我把生命周期方法全打印出来了,待会我们来看看它执行的过程是怎样的。接下来是 Activity,在 Activity 中来启动 IntentService: java view plaincopyprint?1.public class Tes

11、tActivity 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

12、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“);

13、 18. Bundle bundle2 = new Bundle(); 19. bundle2.putString(“param“, “oper2“); 20.startServiceIntent2.putExtras(bundle2); 21.startService(startServiceIntent2); 22. 23. 最后,别忘了配置 Service,因为它继承于 Service,所以,它还是一个 Service,一定要配置,否则是不起作用的,开始我就是忘了,结果半天没反应。 html view plaincopyprint?1. 2. 3. 4. 5. http:/ok,最后来看

14、看执行结果: 从结果可以看到,onCreate 方法只执行了一次,而 onStartCommand 和 onStart 方法执行了两次,开启了两个 Work Thread,这就证实了之前所说的,启动多次,但 IntentService 的实例只有一个,这跟传统的 Service 是一样的。Operation1 也是先于 Operation2 打印,并且我让两个操作间停顿了 2s,最后是 onDestroy 销毁了 IntentService。这就是 IntentService,一个方便我们处理业务流程的类,它是一个 Service,但是比 Service 更智能。希望本文对大家有用,有任何建议或想法欢迎留言和我交流。达内 3G 培训哪里好?成都达内 android 培训班开启企业定制合作满足你高薪就业梦想!找成都 IT 培训 100%推荐就业的成都安卓培训机构,请咨询达内在成都 android 培训的老师!达内培训费用?达内好不好?达内怎么样?达内就业?这些问题都可以在达内的网站上找到答案。在达内科技学习可以申请先就业后付款的方式让刚毕业大学生免除在达内培训费用上的担忧。100%推荐就业更是解决学员培训后的就业问题!达内咨询官网:

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 生活休闲 > 社会民生

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号