gpt4 book ai didi

android - 是否有必要在单独的线程中查找获取位置?

转载 作者:行者123 更新时间:2023-11-29 14:18:04 24 4
gpt4 key购买 nike

我担心在主线程上查找位置信息(使用反向地理编码)会减慢我的 UI。为了解决这个问题,我将信息放入 AsyncTask(下面的代码) 现在我想添加位置监听器,我遇到了各种各样的问题。

我做了一些研究,现在想知道...是否有必要将位置代码放入 AsyncTask 中?也许 Android 只是“自然地”异步查找位置信息?

  public class LocationAsyncTask extends AsyncTask<Void, String, String> {





@Override protected void onPostExecute(String result) {
// TODO Auto-generated method stub
tvLocation.setText(result);

}

@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
tvLocation.setText("Finding current location");

}


@Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);

Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);

String provider = locationManager.getBestProvider(criteria, true);
Location location =locationManager.getLastKnownLocation(provider);
String strUpdateResult = updateWithANewLocation(location);

//locationManager.requestLocationUpdates(provider, 1000, 10, locationListener);


return strUpdateResult;
}

private String updateWithANewLocation(Location location) {
// TODO Auto-generated method stub
StringBuilder sbLocation = new StringBuilder();
String strLocation = new String();
TextView myLocationText;

String addressString = "No address found";
String latLongString = "";

//If there is a location
if (location!= null) {

double latitude = location.getLatitude();
double longitude = location.getLongitude();
double altitude = location.getAltitude();
float accuracy = location.getAccuracy();
float bearing = location.getBearing();
long time = location.getTime();
float speed = location.getSpeed();
String prov = location.getProvider();
strLocation = "Latitude: " + latitude + "\nLongitude: " + longitude + "\n" ;
publishProgress(strLocation);

Geocoder gc = new Geocoder(LateRunner.this, Locale.getDefault());
try {
List<Address> addresses = gc.getFromLocation(latitude, longitude, 1);

//if location and then address is found
if (addresses.size() > 0 ) {
Address address = addresses.get(0);

for (int i=0; i <address.getMaxAddressLineIndex(); i++) {
sbLocation.append(address.getAddressLine(i)).append("\n");
}
strLocation= sbLocation.toString();
publishProgress(strLocation);
}
//if location but no address
else {
strLocation = "Latitude: " + latitude + "\nLongitude: " + longitude + "\n";
publishProgress(strLocation);
} //end try

} catch (IOException e) {
strLocation = "Latitude: " + latitude + "\nLongitude: " + longitude + "\n";
publishProgress(strLocation);

}//end catch
}

//If no location found
else {
strLocation = "Unable to find location. ";
publishProgress(strLocation);
}
return strLocation;



}// end updateWithANewLocation()






@Override protected void onProgressUpdate(String... result) {
// TODO Auto-generated method stub
tvLocation.setText(result[0]);
}
}

最佳答案

我进行了一些快速搜索,但没有看到任何对异步运行的 LocationManager 的具体引用,但我的假设是它确实如此或以不影响 UI 性能的方式安排,因为它是一项系统服务。

根据经验,我可以毫无问题地获取主线程上的位置并将其显示回用户。事实上,我计算了列表中大约 100 个项目的距离,而 UI 没有明显变慢。后一种计算虽然是我认为您应该考虑 AsyncTask 的地方,因为它很容易影响性能。尽管还要注意 AsyncTask 任务的时长和 LocationManager 位置更新的更新间隔。

根据每次调用 updateWithANewLocation 所花费的时间,您可能需要考虑将其置于后台任务中并将 LocationManager 留在主线程中。

我假设您遇到问题的地方是 LocationManager 上的事件处理。以下内容应该可以让您深入了解问题的这一部分:

http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates%28java.lang.String,%20long,%20float,%20android.location.LocationListener%29

The calling thread must be a Looper thread such as the main thread of the calling Activity.

http://developer.android.com/reference/android/os/Looper.html

基本上,当您的 LocationAsyncTask 完成执行时,它就消失了,因此事件回调发生在不再存在的线程上。 Looper 将启动消息循环以接受那些 LocationManager 事件。

关于android - 是否有必要在单独的线程中查找获取位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5690981/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com