我编写了以下代码,以便在收到位置更新时(这是主线程)在线程上进行回调:
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的正确行为是什么?