android - 服务中的位置查找器被信号量阻止?

标签 android android-location java.util.concurrent

我有一项服务需要知道手机的位置。该服务的主要 Activity 在线程中执行如下(删除了处理内容:

Semaphore locationAcquired = new Semaphore(1);
LocationFinder finder;

...
public void run() {
            delaySeconds = 60;
            Looper.prepare();
            while (true) {
                try {

                    finder.StartFinder();

                locationAcquired.acquire();
                // do some stuff...
                } catch (InterruptedException e) {
                    if (isDestroy) {
                        Log.d(TAG, "Closing Monitor Thread");
                        break;
                    } // else just wake up and process the location 
                } catch (Exception e) {
                    e.printStackTrace();

                }
            } // end while
        } // end run

LocationFinder 类实现(同样,稍微简化):

package com.ksdagile.opengate;

import...

public class LocationFinder {

public static final int ONE_SECOND = 1000;

LocationListener locationListener;
String provider = LocationManager.PASSIVE_PROVIDER; // passive by default
LocationManager locationManager;
public Location currentLocation;
long updateSeconds;
private boolean isLooking = false;
OpenGateService openGateService;

public LocationFinder(LocationManager _lm, OpenGateService _openGateService) {

    openGateService = _openGateService;
    locationManager = _lm;
     // initialize with whatever location might be available
    currentLocation = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER); 


    locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            // A new location update is received.  Do something useful with it.  In this case,
            // we're sending the update to a handler which then updates the UI with the new
            // location.
            currentLocation = location;
            String newLoc = String.format("Found new Location lat:%.2f long:%.2f", currentLocation.getLatitude(), currentLocation.getLongitude());
            Log.d(getClass().getName(), newLoc);
            openGateService.locationAcquired.release();
        }
                    // simple implementations of onProvider<> etc.
    };

}

public void SetProvider(boolean isActive) {
    if (isActive)
        provider = LocationManager.GPS_PROVIDER;
    else
        provider = LocationManager.PASSIVE_PROVIDER;
}

public void SetFrequency(long delay) {
    updateSeconds = delay;
}

public void StartFinder() {
    if (!isLooking) {
        isLooking = true;
        locationManager.requestLocationUpdates(provider, updateSeconds*ONE_SECOND, 10, locationListener);
        Log.d(getClass().getName(), String.format("Request location from %s provider, every %d sec.", provider, updateSeconds));            
    } else
        Log.d(getClass().getName(), "Location request running");
}

public void StopFinder() {
    locationManager.removeUpdates(locationListener);
    isLooking = false;
}

public boolean IsLocating() {
    return isLooking;
}
}

我的问题是没有调用 onLocationChanged 例程,即使我知道有新读数。例如,当配置为以被动模式阅读时,我运行 Waze 并看到自己在移动。信号量是否可能阻止对 onLocationChanged 的​​调用?如果是这样,我该如何解决这个问题?我希望能够动态更改 requestLocationUpdate 的参数。

最佳答案

我认为获得风滚草徽章是一种非常可疑的荣誉,但答案如下: 是的,.acquire() 调用会阻塞线程,并且不会调用 onLocationChanged() 方法。

解决方案是让处理程序运行可运行的,例如:

public void onLocationChanged(Location location) {
            xxxService.locHandler.post(xxxService.locationRun);
        }

在哪里

Runnable locationRun = new Runnable() {

    @Override
    public void run() {
        // handle stuff for new location
    }
};

一般原则是您发送和处理事件,而不是运行轮询循环。如果您需要处理非事件,例如没有读取到新的位置,然后你设置了一个倒数计时器。

关于android - 服务中的位置查找器被信号量阻止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18353030/

相关文章:

java - Lambda 表达式对多核系统有哪些优势?

java - ArrayList放入j.u.c.ConcurrentHashMap后会安全发布吗?

android - 您可以使用 Android 上的指纹传感器找到指纹类型(例如食指、中指等)吗?

java - Android:是否有任何Android服务可以在应用程序关闭时执行并且也像绑定(bind)服务一样(持续交互)

android - 获取android中两个位置之间的距离?

android - 为什么我的 OnLocationChanged() 永远不会被调用?

android - 无法解析 Android Studio 上的符号 webView

android - 在 Android 的按钮上画一条线?

Android Linearlayout拼接

java - 我如何在这里整合执行者?或者我该如何改进这个线程池?