1. BroadcastReceiver 在UI thread?
BroadcastReceiver 总是在UI thread,
If you register your BroadcastReceiver
using a valid Handler
running on a different thread:
For example:
1
2
3
4
5
|
HandlerThread handlerThread =
new
HandlerThread(
"ht"
);
handlerThread.start();
Looper looper = handlerThread.getLooper();
Handler handler =
new
Handler(looper);
context.registerReceiver(receiver, filter,
null
, handler);
// Will not run on main thread
|
2. 为什么要使用IntentServie
IntentService是Service的子类,比普通的Service增加了额外的功能。先看Service本身存在两个问题:
-
Service不会专门启动一条单独的进程,Service与它所在应用位于同一个进程中;
-
Service也不是专门一条新线程,因此不应该在Service中直接处理耗时的任务;
IntentService特征
-
会创建独立的worker线程来处理所有的Intent请求;
-
会创建独立的worker线程来处理onHandleIntent()方法实现的代码,无需处理多线程问题;
-
所有请求处理完成后,IntentService会自动停止,无需调用stopSelf()方法停止Service;
-
为Service的onBind()提供默认实现,返回null;
-
为Service的onStartCommand提供默认实现,将请求Intent添加到队列中;
本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/p/6429338.html,如需转载请自行联系原作者