Android 服务中的位置查找器是否被信号量阻止?
我有一项服务需要知道电话的位置。该服务的主要活动在线程中执行,如下所示(去除处理内容:Android 服务中的位置查找器是否被信号量阻止?,android,android-location,java.util.concurrent,Android,Android Location,Java.util.concurrent,我有一项服务需要知道电话的位置。该服务的主要活动在线程中执行,如下所示(去除处理内容: Semaphore locationAcquired = new Semaphore(1); LocationFinder finder; ... public void run() { delaySeconds = 60; Looper.prepare(); while (true) { try {
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;
导入。。。
公共类定位查找器{
公共静态最终整数1秒=1000;
LocationListener LocationListener;
String provider=LocationManager.PASSIVE\u provider;//默认为PASSIVE
地点经理地点经理;
公共场所;
长更新秒;
私有布尔isLooking=false;
OpenGateService OpenGateService;
公共位置查找器(位置管理器、OpenGateService、OpenGateService){
openGateService=\u openGateService;
位置管理器=_lm;
//使用任何可用的位置初始化
currentLocation=locationManager.getLastKnownLocation(locationManager.PASSIVE\u提供程序);
locationListener=新locationListener(){
@凌驾
已更改位置上的公共无效(位置){
//接收到新的位置更新。请对其执行一些有用的操作。在这种情况下,
//我们将更新发送给一个处理程序,该处理程序随后用新的
//地点。
当前位置=位置;
String newLoc=String.format(“找到的新位置纬度:%.2f长:%.2f”,currentLocation.getLatitude(),currentLocation.getLength());
Log.d(getClass().getName(),newLoc);
openGateService.locationAcquired.release();
}
//onProvider等的简单实现。
};
}
公共void集合提供程序(布尔值isActive){
如果(isActive)
provider=LocationManager.GPS\u provider;
其他的
provider=LocationManager.PASSIVE\u provider;
}
公共无效设置频率(长延迟){
updateSeconds=延迟;
}
public void StartFinder(){
如果(!正在查找){
isLooking=正确;
locationManager.RequestLocationUpdate(提供程序,更新秒*1秒,10,locationListener);
Log.d(getClass().getName(),String.format(“从%s提供程序请求位置,每%d秒一次)”,提供程序,updateSeconds));
}否则
Log.d(getClass().getName(),“正在运行位置请求”);
}
公共无效停止查找器(){
locationManager.RemoveUpdate(locationListener);
isLooking=假;
}
公共布尔IsLocating(){
返回搜索;
}
}
我的问题是,即使我知道有新的读数,onLocationChanged例程也不会被调用。例如,当配置为在被动模式下读取时,我运行Waze并看到自己在移动。对onLocationChanged的调用是否可能被信号量阻止?如果是,我如何解决这个问题?我希望能够更改parequestLocationUpdate的参数动态更新。我认为获得风滚草徽章是一项非常可疑的荣誉,但答案如下: 是的,.acquire()调用会阻塞线程,并且不会调用onLocationChanged()方法 解决方案是让处理程序运行runnable,例如:
public void onLocationChanged(Location location) {
xxxService.locHandler.post(xxxService.locationRun);
}
在哪里
Runnable locationRun = new Runnable() {
@Override
public void run() {
// handle stuff for new location
}
};
一般原则是发送和处理事件,而不是运行轮询循环。如果需要处理非事件,例如没有读取新位置,则需要设置倒计时计时器。我认为获得风滚草徽章是一项非常可疑的荣誉,但以下是答案: 是的,.acquire()调用会阻塞线程,并且不会调用onLocationChanged()方法 解决方案是让处理程序运行runnable,例如:
public void onLocationChanged(Location location) {
xxxService.locHandler.post(xxxService.locationRun);
}
在哪里
Runnable locationRun = new Runnable() {
@Override
public void run() {
// handle stuff for new location
}
};
一般原则是发送和处理事件,而不是运行轮询循环。如果需要处理非事件,例如没有读取新位置,则设置倒计时