在Android-Annotations中,@ Background没有创建单独的线程。相反,此代码正在主UI线程中运行。我在以下代码的基础上得出了这个结论: -
@Background
void doGetMoreDishesWishedList() {
Log.d(TAG, "doGetMoreDishesWishedList");
Log.d(TAG, Integer.toString(android.os.Process.myTid()));
if (Looper.myLooper() == Looper.getMainLooper())
Log.d(TAG, "In main loop");
else
Log.d(TAG, "Not In main loop");
}
@UiThread
void updateUiGetMoreDishesWishedList(long requestSentTime, ArrayList<UserDishContainer> newList) {
Log.d(TAG, "updateUiGetMoreDishesWishedList");
Log.d(TAG, Integer.toString(android.os.Process.myTid()));
}
结果是: -
04-07 21:11:33.359: D/WishlistFragment(26346): doGetMoreDishesWishedList
04-07 21:11:33.359: D/WishlistFragment(26346): 26346
04-07 21:11:33.359: D/WishlistFragment(26346): In main loop
04-07 21:11:34.372: D/WishlistFragment(26346): updateUiGetMoreDishesWishedList
04-07 21:11:34.372: D/WishlistFragment(26346): 26346
有没有办法解决这个问题?
答案 0 :(得分:2)
这里有两件事:
Thread.currentThread()
来获取当前线程。要检查它是否是主要线程,请使用以下代码段:Thread.currentThread() == Looper.getMainLooper().getThread()
。