我需要從runnable更新ui。我的邏輯如下。 我從片段生命週期的onCreate開始運行。可運行實例負責請求網絡。問題是我不知道在可運行實例從網絡獲取數據後如何更新片段。如何從Runnable更新UI?
開始在CustomFragment.java的片段中運行的代碼。
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);
}
}
我已經實現了這個評論,一些參考[here](http://stackoverflow.com/a/13954718/3962551) – 2014-12-11 05:45:54