是否有必要在单独的线程中找到获取位置?
|
我担心在主线程上找到位置信息(使用反向地理编码)会减慢我的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]);
}
}
没有找到相关结果
已邀请:
2 个回复
辽躺
遣莫捅炭
对象提供了实例方法
和
,它们都异步工作。调用其中之一后,只需实现回调
即可处理位置更新。我敢打赌Android会做类似的事情。如果没有,请提交错误。