我必须在AsyncTask中使用directions()函数来避免在UI线程上进行网络处理.我无法执行此 *** 作.
我的代码是
public class MainActivity extends MapActivity { @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); MapVIEw mapVIEw = (MapVIEw) findVIEwByID(R.ID.mapvIEw); //or you can declare it directly with the API key Route route = directions(new GeoPoint((int)(26.2*1E6),(int)(50.6*1E6)), new GeoPoint((int)(26.3*1E6),(int)(50.7*1E6))); RouteOverlay routeOverlay = new RouteOverlay(route, color.BLUE); mapVIEw.getoverlays().add(routeOverlay); mapVIEw.invalIDate(); } private Route directions(final GeoPoint start, final GeoPoint dest) { Parser parser; //https://developers.Google.com/maps/documentation/directions/#JsON <- get API String JsonURL = "http://maps.GoogleAPIs.com/maps/API/directions/Json?"; final StringBuffer sBuf = new StringBuffer(JsonURL); sBuf.append("origin="); sBuf.append(start.getLatitudeE6()/1E6); sBuf.append(','); sBuf.append(start.getLongitudeE6()/1E6); sBuf.append("&destination="); sBuf.append(dest.getLatitudeE6()/1E6); sBuf.append(','); sBuf.append(dest.getLongitudeE6()/1E6); sBuf.append("&sensor=true&mode=driving"); parser = new GoogleParser(sBuf.toString()); Route r = parser.parse(); return r; } @OverrIDe public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @OverrIDe protected boolean isRoutedisplayed() { // Todo auto-generated method stub return false; }}
解决方法:
也许是这样吗?
public class MainActivity extends MapActivity { private MapVIEw mapVIEw; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); mapVIEw = (MapVIEw) findVIEwByID(R.ID.mapvIEw); //or you can declare it directly with the API key // You can execute it at onCreate or whenever you want, for example: in a click event new AsyncRoutes().execute(); } private Route directions(final GeoPoint start, final GeoPoint dest) { Parser parser; //https://developers.Google.com/maps/documentation/directions/#JsON <- get API String JsonURL = "http://maps.GoogleAPIs.com/maps/API/directions/Json?"; final StringBuffer sBuf = new StringBuffer(JsonURL); sBuf.append("origin="); sBuf.append(start.getLatitudeE6()/1E6); sBuf.append(','); sBuf.append(start.getLongitudeE6()/1E6); sBuf.append("&destination="); sBuf.append(dest.getLatitudeE6()/1E6); sBuf.append(','); sBuf.append(dest.getLongitudeE6()/1E6); sBuf.append("&sensor=true&mode=driving"); parser = new GoogleParser(sBuf.toString()); Route r = parser.parse(); return r; } @OverrIDe public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @OverrIDe protected boolean isRoutedisplayed() { // Todo auto-generated method stub return false; } private class AsyncRoutes extends AsyncTask<VoID, Integer, Route> { private ProgressDialog pDialog; @OverrIDe protected voID onPreExecute() { pDialog = new ProgressDialog(LoginActivity.this); pDialog.setProgressstyle(ProgressDialog.STYLE_SPINNER); pDialog.setMessage("Obtaining routes..."); pDialog.setCancelable(false); pDialog.show(); } @OverrIDe protected Route doInBackground(VoID... params) { Route route = directions(new GeoPoint((int)(26.2*1E6),(int)(50.6*1E6)), new GeoPoint((int)(26.3*1E6),(int)(50.7*1E6))); return route; } @OverrIDe protected voID onPostExecute(Route route) { RouteOverlay routeOverlay = new RouteOverlay(route, color.BLUE); mapVIEw.getoverlays().add(routeOverlay); mapVIEw.invalIDate(); pDialog.dismiss(); } }}
总结 以上是内存溢出为你收集整理的android-如何在AsyncTask中执行功能?全部内容,希望文章能够帮你解决android-如何在AsyncTask中执行功能?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)