如何在AsyncTask的做功能呢? [英] how to do functions in an AsyncTask?
本文介绍了如何在AsyncTask的做功能呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须使用方向()函数中的AsyncTask避免thread.i'm无法做这件事的UI网络处理。 MY code是
公共类MainActivity扩展MapActivity {
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
图形页面图形页面=(图形页面)findViewById(R.id.mapview); //或者你可以使用API密钥直接将它声明
路线的路线=方向(新的GeoPoint((int)的(26.2 * 1E6),(INT)(50.6 * 1E6)),新的GeoPoint((int)的(26.3 * 1E6),(INT)(50.7 * 1E6)));
RouteOverlay routeOverlay =新RouteOverlay(路线,Color.BLUE);
调用MapView.getOverlays()加(routeOverlay)。
mapView.invalidate();
}
私人路线方向(最终的GeoPoint开始,最终的GeoPoint DEST){
分析器分析器;
//https://developers.google.com/maps/documentation/directions/#JSON< - 获取API
字符串jsonURL =http://maps.googleapis.com/maps/api/directions/json?;
最后的StringBuffer SBUF =新的StringBuffer(jsonURL);
sBuf.append(原产地=);
sBuf.append(start.getLatitudeE6()/ 1E6);
sBuf.append(,);
sBuf.append(start.getLongitudeE6()/ 1E6);
sBuf.append(&放大器;目标=);
sBuf.append(dest.getLatitudeE6()/ 1E6);
sBuf.append(,);
sBuf.append(dest.getLongitudeE6()/ 1E6);
sBuf.append(&放大器,传感器=真放;模式=驱动);
解析器=新GoogleParser(sBuf.toString());
路径R = parser.parse();
返回ř;
}
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
//充气菜单;这增加了项目操作栏,如果它是present。
。getMenuInflater()膨胀(R.menu.activity_main,菜单);
返回true;
}
@覆盖
保护的布尔isRouteDisplayed(){
// TODO自动生成方法存根
返回false;
}
}
解决方案
也许这样吗?
公共类MainActivity扩展MapActivity {
私人图形页面图形页面;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
图形页面=(图形页面)findViewById(R.id.mapview); //或者你可以使用API密钥直接将它声明
//你可以在的onCreate执行它,或者,只要你想,比如:在点击事件
新AsyncRoutes()执行();
}
私人路线方向(最终的GeoPoint开始,最终的GeoPoint DEST){
分析器分析器;
//https://developers.google.com/maps/documentation/directions/#JSON< - 获取API
字符串jsonURL =http://maps.googleapis.com/maps/api/directions/json?;
最后的StringBuffer SBUF =新的StringBuffer(jsonURL);
sBuf.append(原产地=);
sBuf.append(start.getLatitudeE6()/ 1E6);
sBuf.append(,);
sBuf.append(start.getLongitudeE6()/ 1E6);
sBuf.append(&放大器;目标=);
sBuf.append(dest.getLatitudeE6()/ 1E6);
sBuf.append(,);
sBuf.append(dest.getLongitudeE6()/ 1E6);
sBuf.append(&放大器,传感器=真放;模式=驱动);
解析器=新GoogleParser(sBuf.toString());
路径R = parser.parse();
返回ř;
}
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
//充气菜单;这增加了项目操作栏,如果它是present。
。getMenuInflater()膨胀(R.menu.activity_main,菜单);
返回true;
}
@覆盖
保护的布尔isRouteDisplayed(){
// TODO自动生成方法存根
返回false;
}
私有类AsyncRoutes扩展的AsyncTask<太虚,整型,路线> {
私人ProgressDialog pDialog;
@覆盖
在preExecute保护无效(){
pDialog =新ProgressDialog(LoginActivity.this);
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pDialog.setMessage(获取路线......);
pDialog.setCancelable(假);
pDialog.show();
}
@覆盖
保护路由doInBackground(空... PARAMS){
路线的路线=方向(新的GeoPoint((int)的(26.2 * 1E6),(INT)(50.6 * 1E6)),新的GeoPoint((int)的(26.3 * 1E6),(INT)(50.7 * 1E6)));
返回路线;
}
@覆盖
保护无效onPostExecute(路线路线){
RouteOverlay routeOverlay =新RouteOverlay(路线,Color.BLUE);
调用MapView.getOverlays()加(routeOverlay)。
mapView.invalidate();
pDialog.dismiss();
}
}
}
I have to use directions() function in an AsyncTask to avoid network processing on the UI thread.i'm unable to do this thing. MY code is
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;
}
}
解决方案
Perhaps like this?
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();
}
}
}
这篇关于如何在AsyncTask的做功能呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文