![](/img/trans.png)
[英]How to wait for downloading thread to finish and then return without blocking UI thread?
[英]How can I wait for an object filled asynchronously in Android UI thread without blocking it?
我有一個人來處理實體Profilo(配置文件)的注冊和刪除。
通過傳遞標識符並以異步方式在服務器上收集信息來設置此實體。
我的問題是,當必須返回仍未加載的profilo實例時,它將返回null。
public class AccountHandler {
private static AccountHandler istanza = null;
Context context;
private boolean logged;
private Profilo profilo;
private AccountHandler(Context context) {
this.context = context;
//initialization
//setting logged properly
assignField(this.getName());
}
}
public static AccountHandler getAccountHandler(Context context) {
if (istanza == null) {
synchronized (AccountHandler.class) {
if (istanza == null) {
istanza = new AccountHandler(context);
}
}
}
return istanza;
}
public void setAccount(String nickname, String accessingCode) {
logged = true;
assignField(nickname);
}
//other methods
private void assignField(String nickname) {
ProfiloClient profiloClient = new ProfiloClient();
profiloClient.addParam(Profilo.FIELDS[0], nickname);
profiloClient.get(new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode,
Header[] headers,
JSONArray response) {
JSONObject objson = null;
try {
objson = (JSONObject) response.getJSONObject(0);
} catch (JSONException e) {
e.printStackTrace();
}
AccountHandler accountHandler = AccountHandler.getAccountHandler(context);
// Profilo is created with a JSONObject
// **setProfilo is called in async**
**accountHandler.setProfilo(new Profilo(objson));**
}
});
}
private void setProfilo(Profilo profilo) {
this.profilo = profilo;
}
public Profilo getProfilo() {
if( logged && profilo == null)
//How can I wait that profilo is loaded by the JsonHttpResponseHandler before to return it
return this.profilo;
}
}
除了調用getProfilo
您可以使用一種回調機制,在該機制中, AccountHandler
類在加載配置文件時通知調用方。 例如
public void setAccount(String nickname, String accessingCode, MyCallback cb) {
assignField(nickname, cb);
}
private void assignField(String nickname, MyCallback cb) {
....
accountHandler.setProfilo(new Profilo(objson));
cb.onSuccess(this.profilo);
}
在AccountHandler
類中創建一個內部接口MyCallback
(重命名)
public class AccountHandler {
public interface MyCallback {
void onSuccess(Profilo profile);
}
}
現在,無論何時調用setAccount
您都將傳遞回調並在配置文件可用時得到通知,例如
accountHandler.setAccount("Test", "Test", new AccountHandler.MyCallback() {
void onSuccess(Profilio profile) {
// do something with the profile
}
}
正如@Murat K.所建議的那樣,我添加了我的類的接口,該接口將提供一種可以隨時使用的對象調用方法。
public class AccountHandler {
public interface Callback {
void profiloReady(Profilo profilo);
}
}
在處理程序中的getProfilo中調用此方法,該處理程序對getProfilo進行遞歸調用,直到准備使用profilo為止,然后調用該回調方法,該類作為getProfilo的參數傳遞。
public void getProfilo(final Callback Callback) {
if( logged && (profilo == null || !profilo.isReady() ) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
getProfilo(Callback);
}
}, 500);
}else
Callback.profiloReady(profilo);
}
getProfilo調用示例
public class ProfiloCall implements AccountHandler.MyCallback {
@Override
public void profiloReady(Profilo profilo) {
//Use profilo as needed
//EXECUTED ONLY WHEN PROFILO IS READY
}
public void callerMethod() {
//useful code
accountHandler.getProfilo(this);
//other useful code
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.