我编写了以下代码,以便在收到位置更新时在线程上进行回调(这是主线程):
Handler handler; // this Handler is initialized in the following thread
Runnable r = new Runnable() {
public void run() {
Looper.prepare();
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
Log.d("MSG", msg.toString());
}
};
Looper.loop();
}
};
Thread t = new Thread(r);
t.start();
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {
public void onLocationChanged(Location location) {
Log.d("UPD", "onLocationChanged");
}
[...]
}, handler.getLooper());
我希望在收到更新时在日志中看到 MSG。相反,当我看到 UPD 行时,我看不到 MSG。
那么,当我通过 looper 时 requestLocationUpdates 的正确行为是什么?