onLocationChanged(Location loc) & 的问题Android 中的异步任务

发布于 2024-10-20 23:56:33 字数 996 浏览 5 评论 0原文

我有一个连接到互联网的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酷炫老祖宗 2024-10-27 23:56:33
  1. 重写 AsyncTask 的构造函数并将 mylatmylong 设置为该类中的变量
  2. 或将它们直接传递给 doInBackground 方法。

    ... 扩展 AsyncTask {
        受保护的 ReturnType doInBackground(Double... params) {
            mylat = 参数[0];
            mylong = 参数[1];
            列表地址 = geoCoder.getFromLocation(mylat, mylong, 1);
            ...
        }
        ...
    }
    

    并使用execute(mylat, mylong);

    调用它

  1. Override the constructor of your AsyncTask and set mylat and mylong as variables in that class
  2. or pass them to the doInBackground method directly.

    ... extends AsyncTask<Double, ProgressType, ReturnType> {
        protected ReturnType doInBackground(Double... params) {
            mylat = params[0];
            mylong = params[1];
            List addresses = geoCoder.getFromLocation(mylat, mylong, 1);
            ...
        }
        ...
    }
    

    and call it with execute(mylat, mylong);

伴梦长久 2024-10-27 23:56:33

您可以重写 AsyncTask 类的构造函数,将 mylatmylong 作为参数传递。在构造函数中,您将参数保存为 AsyncTask 的成员变量。这样做,您应该能够在 doInBackground() 方法中访问它们。

另一件事是,您应该考虑是否真的需要位置侦听器。也许 LocationManager 及其方法 getLastKnownLocation() 也可以完成这项工作,如果您只需要某些时刻的当前位置。然后您还可以在 doInBackground() 方法中确定当前位置。但请阅读 getLastKnownLocation( )如果它确实符合您的需求。如果每次设备移动时都需要更新,则必须使用位置侦听器。

You can override the constructor of your AsyncTask class in that way that you can pass mylat and mylong 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文