In my last post, I offered some guidelines on writing code to take advantage of multi-core application processors like today’s Qualcomm® Snapdragon™ processors. I mentioned that Java threads are one way of parallelizing tasks, then described AsyncTask, an easier Android construct.

In this post, I’ll describe IntentService, another Android construct that’s easier to implement than Java threads.

Parallelization technique: IntentService

Android provides 'Service' to execute an operation that requires no user interaction. Service runs in the application’s main thread. You can use an IntentService to execute a long-running task in a separate thread without having to handle creation and management of threads.

In your code, you extend the IntentService class and implement the onHandleIntent() method. When the IntentService is triggered using an intent, it spawns a new worker thread and the method onHandleIntent() is called on this thread.

Here is a code snippet showing how to implement an IntentService:

class MyIntentService extends IntentService {
private static final String TAG = "MyIntentService";
public MyIntentService() {
super(TAG);
}

@Override
protected void onHandleIntent(Intent intent) {
//Code to run your task
}
}


Rules

  1. Code running in onHandleIntent() does not block the UI thread.
  2. If multiple intents are received, then they do NOT execute in parallel. They are executed sequentially in the order in which they were received.
  3. After your task has finished executing and there are no intents remaining to be processed, then the IntentService is automatically stopped.

Results can be broadcast back to the application, if required.

When should I use an IntentService for parallelization?

IntentService is designed to use:

  • whenever there is a long task which needs to be performed multiple times, and which does not require user interaction. IntentService provides a very easy way to manage queues for tasks such as accessing a content provider, network operations and processor-intensive math operations.
  • whenever you have a task that you want to trigger in the background without having to launch the UI, and which does not require an always-on Service. For example, you could register a PendingIntent for your IntentService and hence start the IntentService whenever that PendingIntent is fired.

Summary

So, based on the task type, this table summarizes parallelization techniques:

Task Type Parallelization Technique
Short task in the UI thread requiring one thread AsyncTask
Short task in the UI thread requiring a thread pool AsyncTask
Longer task in the UI thread that requires passing messages back and forth Java Threads, Handlers and Loopers
Longer task that does not require user interaction and needs only one worker thread IntentService
Longer task that does not require user interaction and needs a thread pool Java Threads, Handlers and Loopers inside a Service

Your turn

How are you accomplishing parallelization in your own code? Give Java threads, AsyncTask and IntentService a try and let me know what difference you see.Leave any questions or suggestions you have in the comments below.

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Android(安卓)自定义View 新年烟花、横幅
  2. Android仿ios年龄滚轮大全
  3. Android中传送序列化对象出现的ClassNotF
  4. 新浪微博开放平台开发-android客户端(3)
  5. 浅谈Android引用计数(1)
  6. android消息处理机制学习(四)-AsyncTask的
  7. Android开发之旅:应用程序基础及组件
  8. Android(安卓)Bluetooth 4.0深入学习
  9. Android字符串进阶:字体属性及测量(FontMet
  10. ANDROID音频系统散记之五:如何绕开多媒体