Android: LocationManager constructor's looper

2019-04-02 00:43发布

There is the possibility to start retrieving notifications from a LocationManager with the following method:

requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener, Looper looper)

Documentation explains attributes with these words:

provider    the name of the provider with which to register
minTime     minimum time interval between location updates, in milliseconds
minDistance minimum distance between location updates, in meters
listener    a LocationListener whose onLocationChanged(Location) method will be called for each location update
looper      a Looper object whose message queue will be used to implement the callback mechanism, or null to make callbacks on the calling thread

I cannot understand well the behaviour of the class (of the looper) if I'd like to start receiving updates with this method.

Furthermore, I am creating a library around the class LocationManager and, before of performing the normal behaviour, I need to do some other work. Than what I need is to start receiving updates on a library's LocationListener and than perform the normal behaviour only if some conditions are verified.

In order to do this I need to know how to simulate the behaviour that would have the LocationManager if the user started to receive updates with the overmentioned method.

I hope I am clear. Can someone help me? Thanks!

1条回答
SAY GOODBYE
2楼-- · 2019-04-02 01:25

A Looper is basically a thread that runs in the background and does work whenever it receives message or runnable from a Handler object. The main looper is part of the UI thread. Other loopers are usually created by contructing new HandlerThread and then calling thread.start(), followed by thread.getLooper().

LocationManager allows you to request location with a specific Looper or on the main Looper (UI thread).

requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener, Looper looper)

or call

requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)

Internally in Android Location manager it sets up a ListenerTransport object to create a Handler for the looper provided or on the main thread if none is provided. This handler receives the listener events from the LocationManager providers.

Typically you will request updates to your listener with a Looper when you want to process location manager events inside an AsyncTask or if you want to perform long running operations inside your listener and avoid blocking the UI thread. A simple example follows:

HandlerThread t = new HandlerThread("my handlerthread");
t.start();
locationManager.requestLocationUpdates(locationManager.getBestProvider(), 1000l, 0.0f, listener, t.getLooper());

Within your LocationLiistener

Handler MAIN_HANDLER = new Handler(Looper.getMainLooper());
@Override
public void onLocationChanged(Location location) {
     final MyPojoResult result = doSomeLongRuningOperation(location);
     MAIN_HANDLER.post( new Runnable() { 
        @Override
        public void run() {
           doSomeOperationOnUIThread(result):
        }
     });
}
查看更多
登录 后发表回答