出于某种原因,这个类做太多的工作和滞后,这是我的应用程序上下文中的一个主要问题。当用户单击地图上的某个位置时,它应该快速获取该位置的地址,但不幸的是我让 logCat dalvikvm 暂停,内存释放消息和线程在应用程序上做了太多工作。不知道为什么。我在打开数据库连接后关闭它们。
private class GeocoderTask extends AsyncTask<String, Void, List<Address>> {
@Override
protected List<Address> doInBackground(String... locationName) {
Geocoder geocoder = new Geocoder(getBaseContext());
List<Address> addresses = null;
try {
// get maximum of 3 addresses that match input text
addresses = geocoder.getFromLocationName(locationName[0], 3);
} catch (IOException e) {
e.printStackTrace();
}
return addresses;
}
@Override
protected void onPostExecute(List<Address> addresses) {
if (addresses == null || addresses.size() == 0) {
Toast.makeText(getApplicationContext(), "No location found",
Toast.LENGTH_SHORT).show();
}
// clear existing markers on map
map.clear();
// add markers on map for each matching address
for (int i = 0; i < addresses.size(); i++) {
Address address = (Address) addresses.get(i);
// creating an instance of geoPoint to display in googleMap
latLng = new LatLng(address.getLatitude(),
address.getLongitude());
String addressText = String.format(
"%s, %s",
address.getMaxAddressLineIndex() > 0 ? address
.getAddressLine(0) : "", address
.getCountryName());
markerOp = new MarkerOptions();
markerOp.position(latLng);
markerOp.title(addressText);
map.addMarker(markerOp);
// Locate the first location
if (i == 0)
map.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
}
}
/**
* Locates address using LatLng coordinates
* @author Aaron
*
*/
private class ReverseGeocodingTask extends AsyncTask<LatLng, Void, String> {
Context mContext;
public ReverseGeocodingTask(Context context) {
super();
mContext = context;
}
// Finding address using reverse geocoding
@Override
protected String doInBackground(LatLng... params) {
Geocoder geocoder = new Geocoder(mContext);
double latitude = params[0].latitude;
double longitude = params[0].longitude;
List<Address> addresses = null;
String addressText = "";
try {
addresses = geocoder.getFromLocation(latitude, longitude, 3);
Thread.sleep(500);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
e.printStackTrace();
}
if (addresses != null && addresses.size() > 0) {
Address address = addresses.get(0);
addressText = String.format(
"%s, %s, %s",
address.getMaxAddressLineIndex() > 0 ? address
.getAddressLine(0) : "", address.getLocality(),
address.getCountryName());
}
return addressText;
}
@Override
protected void onPostExecute(String addressText) {
// Setting the title for the marker.
// This will be displayed on taping the marker
markerOp.title(addressText);
// Placing a marker on the touched position
map.addMarker(markerOp);
}
}
而Logcat产生
03-04 16:47:38.037: I/Choreographer(16424): Skipped 39 frames! The application may be doing too much work on its main thread.
03-04 16:47:38.287: W/ActivityThread(16424): ClassLoader.loadClass: The class loader returned by Thread.getContextClassLoader() may fail for processes that host multiple applications. You should explicitly specify a context class loader. For example: Thread.setContextClassLoader(getClass().getClassLoader());
03-04 16:47:38.407: D/dalvikvm(16424): GC_FOR_ALLOC freed 1017K, 14% free 11702K/13476K, paused 48ms, total 49ms
03-04 16:47:38.637: D/dalvikvm(16424): GC_FOR_ALLOC freed 1052K, 12% free 11965K/13476K, paused 25ms, total 25ms
03-04 16:47:38.907: D/dalvikvm(16424): GC_FOR_ALLOC freed 1101K, 9% free 12300K/13476K, paused 25ms, total 25ms
03-04 16:47:39.277: D/dalvikvm(16424): GC_FOR_ALLOC freed 1330K, 10% free 12526K/13892K, paused 28ms, total 28ms
03-04 16:47:39.537: D/dalvikvm(16424): GC_FOR_ALLOC freed 1486K, 11% free 12667K/14192K, paused 36ms, total 36ms
03-04 16:47:39.757: D/dalvikvm(16424): GC_FOR_ALLOC freed 1324K, 10% free 13025K/14384K, paused 32ms, total 32ms
03-04 16:47:40.037: D/dalvikvm(16424): GC_FOR_ALLOC freed 1774K, 13% free 13039K/14852K, paused 52ms, total 55ms
03-04 16:47:40.107: D/dalvikvm(16424): GC_FOR_ALLOC freed 29K, 12% free 13178K/14852K, paused 27ms, total 27ms
03-04 16:47:40.337: D/dalvikvm(16424): GC_FOR_ALLOC freed 906K, 11% free 13477K/15040K, paused 32ms, total 33ms
03-04 16:47:40.417: I/dalvikvm(16424): Jit: resizing JitTable from 4096 to 8192
03-04 16:47:40.607: D/dalvikvm(16424): GC_FOR_ALLOC freed 1098K, 14% free 13388K/15448K, paused 28ms, total 28ms
03-04 16:47:42.687: D/dalvikvm(16424): GC_FOR_ALLOC freed 1307K, 14% free 13429K/15448K, paused 30ms, total 30ms
03-04 16:47:42.687: I/dalvikvm-heap(16424): Grow heap (frag case) to 14.147MB for 1048592-byte allocation
03-04 16:47:42.717: D/dalvikvm(16424): GC_FOR_ALLOC freed 5K, 13% free 14448K/16476K, paused 28ms, total 28ms
03-04 16:47:42.787: D/dalvikvm(16424): GC_FOR_ALLOC freed 1073K, 19% free 13418K/16476K, paused 29ms, total 29ms
03-04 16:47:43.217: D/dalvikvm(16424): GC_FOR_ALLOC freed 1550K, 19% free 13425K/16476K, paused 29ms, total 29ms
03-04 16:47:43.527: D/dalvikvm(16424): GC_FOR_ALLOC freed 1657K, 17% free 13685K/16476K, paused 30ms, total 30ms
03-04 16:47:44.337: D/dalvikvm(16424): GC_FOR_ALLOC freed 1620K, 15% free 14084K/16476K, paused 34ms, total 34ms
03-04 16:47:46.547: D/dalvikvm(16424): GC_FOR_ALLOC freed 2071K, 15% free 14140K/16476K, paused 37ms, total 37ms
03-04 16:47:52.147: D/dalvikvm(16424): GC_FOR_ALLOC freed 2585K, 18% free 13636K/16476K, paused 38ms, total 39ms
03-04 16:47:54.737: D/dalvikvm(16424): GC_FOR_ALLOC freed 1834K, 17% free 13804K/16476K, paused 48ms, total 48ms
03-04 16:48:05.387: D/dalvikvm(16424): GC_FOR_ALLOC freed 2096K, 17% free 13766K/16476K, paused 50ms, total 50ms
03-04 16:48:51.197: D/dalvikvm(16424): GC_FOR_ALLOC freed 2062K, 17% free 13750K/16476K, paused 43ms, total 46ms
03-04 16:48:55.627: D/dalvikvm(16424): GC_FOR_ALLOC freed 2033K, 17% free 13757K/16476K, paused 40ms, total 40ms
03-04 16:49:45.877: D/dalvikvm(16424): GC_FOR_ALLOC freed 2055K, 17% free 13745K/16476K, paused 41ms, total 42ms
03-04 16:50:31.287: D/dalvikvm(16424): GC_FOR_ALLOC freed 2041K, 17% free 13744K/16476K, paused 38ms, total 38ms
03-04 16:51:21.397: D/dalvikvm(16424): GC_FOR_ALLOC freed 2038K, 17% free 13744K/16476K, paused 37ms, total 37ms
03-04 16:52:06.817: D/dalvikvm(16424): GC_FOR_ALLOC freed 2040K, 17% free 13744K/16476K, paused 38ms, total 38ms
03-04 16:52:56.817: D/dalvikvm(16424): GC_FOR_ALLOC freed 2038K, 17% free 13744K/16476K, paused 39ms, total 39ms
03-04 16:53:42.307: D/dalvikvm(16424): GC_FOR_ALLOC freed 2039K, 17% free 13744K/16476K, paused 39ms, total 39ms
03-04 16:54:32.277: D/dalvikvm(16424): GC_FOR_ALLOC freed 2038K, 17% free 13744K/16476K, paused 90ms, total 90ms