Part 2: Multi-threading-android-apps-multi-core...
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
- Code running in onHandleIntent() does not block the UI thread.
- If multiple intents are received, then they do NOT execute in parallel. They are executed sequentially in the order in which they were received.
- 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.
更多相关文章
- 代码中设置drawableleft
- android 3.0 隐藏 系统标题栏
- Android开发中activity切换动画的实现
- Android(安卓)学习 笔记_05. 文件下载
- Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
- 技术博客汇总
- android 2.3 wifi (一)
- AndRoid Notification的清空和修改
- Android中的Chronometer