onLocationChanged(Location loc) & 的问题Android 中的异步任务
我有一个连接到互联网的 Android 应用程序,可以实时对纬度和经度坐标进行反向地理编码。这会延迟 UI,并可能导致它给出“未响应 - 强制关闭/等待”错误(如预期,不使用异步/线程处理程序!)
我已经实现了一个工作得很好的异步任务(不再有 UI 延迟!) ,但在 onLocationChanged 方法中设置的变量(将它们设置为纬度和经度)是无法访问的。 我正在执行此操作的类链接到另一个单独的类,该类传递变量并可以向包含反向地理编码地址的人发送短信。 但是,由于使用 doinbackground 方法实现异步任务,我无法再发送我需要的内容。我不知道是否可以更好地解释,我将提供示例代码。
public void onLocationChanged(Location loc){
mylat = loc.getLatitude();
mylong = loc.getLongitude();
}
/*
afaik, this is the code that does the internet stuff, that lags the
UI. If I put this in doinbackground, it cannot access mylat & my long,
and the "loc.getLatitude" has to be done in the "onLocationChanged"
method, which HAS to exist. If I leave the ListAddress code outside the
doinbackground, everything works, BUT the UI will obviously lag.
*/
List<Address> addresses = geoCoder.getFromLocation(mylat,mylong, 1);
任何人都可以建议获取 mylat = loc.getLatitude(); 的最佳方法吗? & mylat = loc.getLatitude(); 在 doinbackground 方法中?
I have an android app that connects to the internet to do a reverse geocode on lat and long co-ordinates in real time. This lags the UI and can cause it to give a "not responding - force close/wait" error(as expected without using async/ threads-handlers!)
I have implemented an Async Task which works very well(no more UI lag!), but variables set inside the onLocationChanged method (that sets them to the latitude & longitude) are inaccessible.
The class I am doing this in, is linked to another seperate class that passes the variables and can send an sms to a person which includes the reverse-geocoded address.
However, since implementing the Async Task using the doinbackground method, I can no longer send what I need. I don't know if I can explain any better, I will provide sample code.
public void onLocationChanged(Location loc){
mylat = loc.getLatitude();
mylong = loc.getLongitude();
}
/*
afaik, this is the code that does the internet stuff, that lags the
UI. If I put this in doinbackground, it cannot access mylat & my long,
and the "loc.getLatitude" has to be done in the "onLocationChanged"
method, which HAS to exist. If I leave the ListAddress code outside the
doinbackground, everything works, BUT the UI will obviously lag.
*/
List<Address> addresses = geoCoder.getFromLocation(mylat,mylong, 1);
Can anyone advise the best way to get mylat = loc.getLatitude(); & mylat = loc.getLatitude();
inside the doinbackground method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
mylat
和mylong
设置为该类中的变量或将它们直接传递给 doInBackground 方法。
并使用
execute(mylat, mylong);
调用它
mylat
andmylong
as variables in that classor pass them to the doInBackground method directly.
and call it with
execute(mylat, mylong);
您可以重写 AsyncTask 类的构造函数,将
mylat
和mylong
作为参数传递。在构造函数中,您将参数保存为 AsyncTask 的成员变量。这样做,您应该能够在 doInBackground() 方法中访问它们。另一件事是,您应该考虑是否真的需要位置侦听器。也许 LocationManager 及其方法 getLastKnownLocation() 也可以完成这项工作,如果您只需要某些时刻的当前位置。然后您还可以在 doInBackground() 方法中确定当前位置。但请阅读 getLastKnownLocation( )如果它确实符合您的需求。如果每次设备移动时都需要更新,则必须使用位置侦听器。
You can override the constructor of your AsyncTask class in that way that you can pass
mylat
andmylong
as parameters. In the constructor you save the parameters as member variables of the AsyncTask. Doing this, you should be able to access them within the doInBackground() method.Another thing is, you should think about if you really need a location listener. Perhaps the LocationManager and its method getLastKnownLocation() will also do the job, if you only need the current location on certain moments. Then you can also determine the current position within the doInBackground() method. But be please read the documentation of getLastKnownLocation() if it really fits your needs. If you need an update everytime the device moves, you will have to use the location listener.