I wrote the following code, in order to have callbacks on a thread when a location update is received (this is the main thread):
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());
I am expecting to see MSG in Log when an update is received. Instead, while I see the UPD row, I cannot see MSG.
Then, what is the correct behavior of the requestLocationUpdates when I pass a looper?