我有一個在UI線程中註冊的BroadcastReceiver,它在onReceive方法中從Bundle獲取一些信息。在我進入主線程之前,我需要這些值。如何等待onReceive在同一個線程中完成?
在嘗試使用這些值之前是否有任何方法等待onReceive完成?我遇到了在嘗試使用它們之後onReceive設置值的計時問題。讓線程休眠不起作用,因爲它們在同一個線程中。
將接收器註冊到AsyncTask中,並在主線程中調用wait(),然後在onReceive notify()完成後是否有意義?
String a = "hi";
IntentFilter filter = new IntentFilter();
filter.addAction(MY_CUSTOM_INTENT);
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Set the variable values here
a = "bye";
}
};
getApplicationContext().registerReceiver(receiver, filter);
// Get the values, I am getting a = "hi" here because the onReceive code has
// not been reached yet
// How can I guarantee that a = "bye" from this method?
getA();
其中,方法是一樣的東西
String getA() {
return a;
}
你能說清楚嗎? – pawelzieba 2012-07-11 19:18:15