Android异步载入全解析之IntentService
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Android异步载入全解析之IntentService,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3578字,纯文字阅读大概需要6分钟。
内容图文
Android异步载入全解析之IntentService
搞什么IntentService
- 不能够直接和UI做交互。为了把他运行的结果体如今UI上。须要发送给Activity。
- 工作任务队列是顺序运行的,假设一个任务正在IntentService中运行,此时你再发送一个任务请求,这个任务会一直等待直到前面一个任务运行完成。
- 正在运行的任务无法打断。
创建IntentService
package com.imooc.intentservicetest; import android.app.IntentService; import android.content.Intent; public class MyIntentService extends IntentService { /** * Creates an IntentService. Invoked by your subclass‘s constructor. * * @param name Used to name the worker thread, important only for debugging. */ public MyIntentService(String name) { super(name); } public MyIntentService() { super("MyIntentService"); } @Override protected void onHandleIntent(Intent intent) { } }
PS:须要注意的是,系统会提示我们生成一个带參数的构造方法,另外。我们还须要定义一个无參数的构造方法,并调用一个參数的构造方法。所以。一个带參数的构造方法事实上是能够删掉的。
@Override protected void onHandleIntent(Intent intent) { }
在这种方法里面,我们从intent中获取数据,并进行对应的操作。
申明IntentService
<service android:name=".MyIntentService" android:exported="false"/>
启动IntentService
package com.imooc.intentservicetest; import android.app.Activity; import android.content.Intent; import android.os.Bundle; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = null; for (int i = 0; i < 10; i++) { intent = new Intent(this, MyIntentService.class); intent.putExtra("xys", "" + i); startService(intent); } } }
能够看见,我们start了10个IntentService,最后运行的结果例如以下:
05-13 17:14:53.515 19991-20011/com.imooc.intentservicetest D/xys﹕ onHandleIntent0 05-13 17:14:55.528 19991-20011/com.imooc.intentservicetest D/xys﹕ onHandleIntent1 05-13 17:14:57.540 19991-20011/com.imooc.intentservicetest D/xys﹕ onHandleIntent2 05-13 17:14:59.544 19991-20011/com.imooc.intentservicetest D/xys﹕ onHandleIntent3 05-13 17:15:01.556 19991-20011/com.imooc.intentservicetest D/xys﹕ onHandleIntent4 05-13 17:15:03.569 19991-20011/com.imooc.intentservicetest D/xys﹕ onHandleIntent5 05-13 17:15:05.570 19991-20011/com.imooc.intentservicetest D/xys﹕ onHandleIntent6 05-13 17:15:07.574 19991-20011/com.imooc.intentservicetest D/xys﹕ onHandleIntent7 05-13 17:15:09.577 19991-20011/com.imooc.intentservicetest D/xys﹕ onHandleIntent8 05-13 17:15:11.581 19991-20011/com.imooc.intentservicetest D/xys﹕ onHandleIntent9
所以说,启动IntentService之后。它将默认生成一个Worker Thread,并将这些IntentService依次放入队列,每次取出一个进行运行,当运行完成后。这个IntentService会自己主动stop自己。当全部intent都运行完成后,服务就结束了,不须要自己手动来结束。
IntentService改动UI
IntentService与AsyncTask的差别
- 对于异步更新UI来说,IntentService使用的是Serivce+handler或者广播的方式,而AsyncTask是thread+handler的方式。
- AsyncTask比IntentService更加轻量级一点。
- Thread的运行独立于Activity,当Activity结束之后。假设没有结束thread,那么这个Activity将不再持有该thread的引用。
- Service不能在onStart方法中运行耗时操作,仅仅能放在子线程中进行处理,当有新的intent请求过来都会线onStartCommond将其入队列,当第一个耗时操作结束后,就会处理下一个耗时操作(此时调用onHandleIntent)。都运行完了自己主动运行onDestory销毁IntengService服务。
原文:http://www.cnblogs.com/mengfanrong/p/5080600.html
内容总结
以上是互联网集市为您收集整理的Android异步载入全解析之IntentService全部内容,希望文章能够帮你解决Android异步载入全解析之IntentService所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。