我有一个 IntentService 来对我的 API 执行后台请求。我正在使用 Otto Bus 与之通信。
public class MyService extends IntentService {
private MyAPI mApi;
private MyBus mBus;
MyService () {
super("MyService ");
}
@Subscribe
public void onLoadSearchData(LoadSearchDataEvent event) {
Log.d("onLoadSearchData "+ Thread.currentThread().getName());
mApi.loadSomeData();
}
@Override
protected void onHandleIntent(Intent intent) {
Thread.currentThread().setName(getClass().getCanonicalName());
Log.d("Thread name " + Thread.currentThread().getName());
if (mApi==null) mApi = new MyAPI(getApplicationContext());
if (mBus==null) {
mBus = MyBus.getInstance();
mBus.register(this);
}
}
}
onHandleIntent 在辅助线程上执行,这是正常的。但是当我 从主用户界面使用总线事件调用onLoadSearchData时,它在用户界面线程上运行!!!!
我不明白为什么。
我的目的是有一个后台线程来加载/缓存数据。
不知道该怎么做。谢谢您的帮助。