-->

安卓的LocationManager构造的尺蠖(Android: LocationManager c

2019-08-02 19:06发布

还有就是从开始用下面的方法的LocationManager获取通知的可能性:

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

文档解释了这句话的属性:

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

我不能很好地理解类(活套)的行为,如果我想开始接收这种方法的更新。

此外,我周围产生类的LocationManager图书馆,并进行正常的行为之前,我需要做一些其他工作。 比我需要的是开始一个图书馆的LocationListener的,比只执行如果有些条件已被验证的正常行为接收更新。

为了做到这一点,我需要知道如何以模拟将有的LocationManager如果用户开始与overmentioned方法接收更新的行为。

我希望我是清楚的。 有人能帮我吗? 谢谢!

Answer 1:

活套基本上是在后台运行并且每当它接收到来自一个Handler对象消息或可运行确实工作的线程。 主要是尺蠖UI线程的一部分。 ()其他活套通常由contructing新HandlerThread,然后调用thread.start()创建的,其次是thread.getLooper。

的LocationManager允许您与特定的活套或主尺蠖(UI线程)请求位置。

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

或致电

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

在内部的Android位置管理它建立了一个ListenerTransport对象所提供的弯针或在主线程创建一个处理程序。如果没有提供。 该处理器接收来自供应商的LocationManager监听事件。

一般来说,当你要处理一个的AsyncTask里面,或者如果你想执行你的监听器里长时间运行操作和避免阻塞UI线程位置管理事件,你会要求更新与活套监听器。 一个简单的例子如下:

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

在您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):
        }
     });
}


文章来源: Android: LocationManager constructor's looper