Android TimerTask 获取 GPS 位置冻结
我正在与 Android 中的 Looper 作斗争。我有一个每分钟运行一次的计时器。这将向服务器发送一条包含用户位置的消息。
private Looper looper;
public boolean getLocation(Context context, LocationResult result) {
locationResult = result;
lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
//exceptions will be thrown if provider is not permitted.
try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
}
// don't start listeners if no provider is enabled
if (!gps_enabled)
return false;
if(Looper.myLooper() == null)
Looper.myLooper().prepare();
looper = Looper.myLooper();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
looper.loop();
return true;
}
LocationListener locationListenerGps = new LocationListener() {
public void onLocationChanged(Location location) {
looper.quit();
lm.removeUpdates(this);
locationResult.gotLocation(location); // broadcast location
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
计时器第一次启动时,一切都运行良好。第二次,计时器冻结,手机上的 GPS 图标显示已锁定,但并未消失。就好像 Looper 第二次没有循环处理消息,即使我告诉它循环。如果我每次都调用 Looper.prepare() ,那么第二次计时器执行时,我会得到一个异常,说每个线程只有一个 Looper。
当然这不应该那么难!
I'm struggling with this Looper in Android. I have a timer that runs every minute. This sends a message to the server with the user's location.
private Looper looper;
public boolean getLocation(Context context, LocationResult result) {
locationResult = result;
lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
//exceptions will be thrown if provider is not permitted.
try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
}
// don't start listeners if no provider is enabled
if (!gps_enabled)
return false;
if(Looper.myLooper() == null)
Looper.myLooper().prepare();
looper = Looper.myLooper();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
looper.loop();
return true;
}
LocationListener locationListenerGps = new LocationListener() {
public void onLocationChanged(Location location) {
looper.quit();
lm.removeUpdates(this);
locationResult.gotLocation(location); // broadcast location
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
The first time the timer fires, everything works perfectly. The second time, the timer freezes and the GPS icon on the phone says it has a lock but does not go away. It's as if the Looper is not looping to handle the messages the second time around, even though I tell it to loop. If I call Looper.prepare() every time, the second time the timer executes I get the exception saying only one Looper per thread.
Surely this shouldn't be so hard!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是最好的方法,但是当计时器计时时,它会创建一个新线程,在该线程上获取当前位置,然后将该信息发送到服务器。这样每个线程都会得到自己的 Looper 来解决我的问题。正如我所说,不是最好的,但计时器确实还有其他工作要做。
Not the best way, but when the timer ticks, it creates a new Thread on which to get the current location and then send that info to the server. In this way each thread will will gets its own Looper which fixes my problem. As I said, not the best, but the timer does have other work to do.