如何在超时情况下从 LocationManager 获取单个位置修复?
我正在使用 LocationManager 来获取单个位置修复:
public class MyActivity extends Activity {
private LocationManager lm;
private ProgressDialog myDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new MyLocationListener();
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 1, ll);
myDialog = ProgressDialog.show(this, null , "Determining Your Location", true);
}
class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
if (location != null) {
...
lm.removeUpdates(this);
myDialog.dismiss();
}
}
}
}
如果找不到位置修复,则监听位置可能会永远持续下去。我想通过在 60 秒后停止侦听位置并向用户显示错误来说明无法确定其位置来为我的代码添加一些稳健性。
我该怎么做?
I am using LocationManager
to get a single location fix:
public class MyActivity extends Activity {
private LocationManager lm;
private ProgressDialog myDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new MyLocationListener();
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 1, ll);
myDialog = ProgressDialog.show(this, null , "Determining Your Location", true);
}
class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
if (location != null) {
...
lm.removeUpdates(this);
myDialog.dismiss();
}
}
}
}
As this stands listening for a location could potentially go on forever if a location fix cannot be found. I want to add some robustness to my code by ceasing to listening for a location after 60 seconds, and displaying an error to the user to saying that their location could not be determined.
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 Handler、计时器 或 AlarmManager 通过调用
LocationManager.removeUpdates
停止监听来实现超时。Timer
创建一个新线程,这可能有点过头了。AlarmManager
的文档表明“或者正常的计时操作(滴答声、超时等),使用 Handler 更容易、更高效”。Handler
的文档描述了Handler
的主要用途之一是“安排消息和可运行对象在未来某个时刻执行(原文如此)”。所有迹象都表明
Handler
是实现超时的最合适方法。注意:
TIMEOUT
当然是超时长度(以毫秒为单位)。在本例中为 60,000 毫秒,即 60 秒。我选择在
MyActivity
本身上实现LocationListener
接口,以便在Runnable
中更容易访问LocationListener
。而不是调用
LocationManager.requestLocationUpdates
:我正在使用LocationManager.requestSingleUpdate
它将仅提供一个位置修复。我故意没有实现
Activity.onPause
和Activity.onResume
。如果活动暂停,位置侦听和超时都将继续。You could either user a Handler, Timer or AlarmManager to implement the timeout by calling
LocationManager.removeUpdates
to stop listening.A
Timer
creates an new thread, which may be overkill. The documentation forAlarmManager
suggests that "(f)or normal timing operations (ticks, timeouts, etc) it is easier and much more efficient to use Handler". The documentation forHandler
describes one of the main uses for aHandler
is "to schedule messages and runnables to be executed as (sic) some point in the future."All signs point to
Handler
as being the most appropriate method of implementing the timeout.NOTES:
TIMEOUT
is, of course, the timeout length in milliseconds. In this case 60,000 milliseconds, or 60 seconds.I have chosen to implement the
LocationListener
interface onMyActivity
itself so that theLocationListener
is easier to access within theRunnable
.Instead of calling
LocationManager.requestLocationUpdates
: I'm usingLocationManager.requestSingleUpdate
which will supply just one location fix.I deliberately haven't implemented
Activity.onPause
andActivity.onResume
. Both the location listening, and timeout, will continue if the Activity is paused.一种方法是使用 Handler 并在 60 秒后使用 postDelayed 来停止侦听器。
处理程序 postDelayed
One way would be to use a Handler and use postDelayed after 60 sec to stop the listener.
Handler postDelayed
您也可以使用 Timer 和 TimerTask。当您创建处理程序以利用 postDelayed 时,您应该小心内存泄漏,因为处理程序在完成后仍会引用您的 Activity 或 Service 事件。 PostDelayed 将进入主线程中的消息队列。因此,请将您的处理程序设置为静态或使用弱引用。
您需要考虑以下代码以避免内存泄漏。
You can use Timer and TimerTask as well. When you create handler to utilise postDelayed, you should be careful of memory leak because the handler still refer your Activity or Service event after they are already finished. PostDelayed will be going to the message queue in the main thread. So, make your handler as static or use weak reference.
You need to consider following code to avoid the memory leak.