我需要从Runnable更新UI。我的逻辑如下。
我在片段生命周期的onCreate中启动了runnable。而且,runnable实例负责请求网络。问题是当runnable实例从网络获取数据后,我不知道如何更新片段。
以下是CustomFragment.java中在片段中启动runnable的代码。
public void onCreate(Bundle savedInstanceState) {
Log.d(DEBUG_TAG, "onCreate");
super.onCreate(savedInstanceState);
accountMgr.requestAccountInfo();
}
在AccountManager.java中启动可运行代码的代码:
/**
* request Account info from server
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void requestAccountInfo() {
Account act = getCurrentAccount();
Thread t = new Thread(new RequestAccountInfoTask(act));
t.start();
}
/**
* automatically update Account info, like space usage, total space size, from background.
*/
class RequestAccountInfoTask implements Runnable {
private Account account;
public RequestAccountInfoTask(Account account) {
this.account = account;
}
@Override
public void run() {
doRequestAccountInfo(account);
}
}