Android:LocationManager构造函数的循环程序 [英] Android: LocationManager constructor's looper

查看:125
本文介绍了Android:LocationManager构造函数的循环程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以使用以下方法开始从LocationManager检索通知:

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.

此外,我正在围绕LocationManager类创建一个库,在执行正常行为之前,我需要做一些其他工作.比我需要的是开始在库的LocationListener上接收更新,然后仅在验证某些条件后才执行正常行为.

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.

为此,我需要知道如何模拟如果用户开始使用上述方法接收更新时将具有LocationManager的行为.

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!

推荐答案

Looper基本上是一个在后台运行的线程,只要它从Handler对象接收消息或可运行,它就可以工作.主循环程序是UI线程的一部分.通常通过构造新的HandlerThread然后调用thread.start(),然后调用thread.getLooper()来创建其他循环程序.

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允许您使用特定的Looper或在主Looper(UI线程)上请求位置.

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)

或致电

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

在Android位置管理器内部,它设置了ListenerTransport对象以为提供的循环程序或主线程(如果未提供)创建处理程序.该处理程序从LocationManager提供程序接收侦听器事件.

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.

通常,当您要在AsyncTask中处理位置管理器事件,或者要在监听器中执行长时间运行的操作并避免阻塞UI线程时,将使用Looper请求对监听器进行更新.一个简单的例子如下:

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());

在LocationLiistener内

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

这篇关于Android:LocationManager构造函数的循环程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆