2013-03-13 87 views
1

我必須在AsyncTask中使用directions()函數來避免UI線程上的網絡處理。我無法做到這一點。 我的代碼是如何在AsyncTask中執行函數?

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; 
    } 

} 
+0

有什麼問題? – stinepike 2013-03-13 04:56:06

+0

我不確定我是否正確地閱讀了您的代碼,但我假設您希望指示成爲AsyncTask。那是對的嗎? – 2013-03-13 04:56:31

+0

http://developer.android.com/reference/android/os/AsyncTask.html – 2013-03-13 05:02:25

回答

1

也許這樣?

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(); 
     } 
    } 

} 
+0

thanks.I'm使用下面的鏈接在Android的駕駛方向,他們說只是在asynctask中使用direction()函數,但仍然不能停止應用程序..可以幫助我。 http://stackoverflow.com/questions/11745314/why-retrieving-google-directions-for-android-using-kml-data-is-not-working-anymo/11745316#11745316 – 2013-03-13 05:10:52

+0

然後,請提供LogCat輸出要知道問題出在哪裏。 – Areks 2013-03-13 05:12:43

+0

日誌貓顯示這個 03-13 10:43:15.129:E /索引0超出範圍[0..0)(18918):Google JSON解析器 - http://maps.googleapis.com/maps/api /directions/json?origin=26.2,50.6&destination=26.3,50.7&sensor=true&mode=driving 03-13 10:43:15.182:E/AndroidRuntime(18918):致命例外:main – 2013-03-13 05:15:20

0

這是一個AsyncTask

public class TalkToServer extends AsyncTask<String, String, String> { 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
    } 

    @Override 
    protected void onProgressUpdate(String... values) { 
     super.onProgressUpdate(values); 

    } 

    @Override 
    protected String doInBackground(String... params) { 
    //do your work here 
     return something; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 
      // do something with data here-display it or send to mainactivity 
} 

的基本結構,你可以做你的網絡的東西和/或東西,你在doInBackground()directions和更新其他方法UI。你也可以把directions方法的私有方法您AsyncTask

那麼你會從你的MainActivity稱此像

TalkToServer networkStuff = new TalkToServer(); //could pass params to a constructor here such as context 
networkStuff.execute(); 

如果AsyncTask不是一個內部類的MainActivity,那麼你就需要構造函數從您的MainActivity收到背景下,如果你想要做的東西UI

+0

謝謝。我'm使用下面的鏈接在Android的駕駛方向,他們說只是在asynctask中使用direction()函數,但仍然不能停止應用程序..可以幫助我。 http:// stackoverflow。com/questions/11745314/why-retrieval-google-directions-for-android-using-kml-data-is-not-working-anymo/11745316#11745316 – 2013-03-13 05:48:09

+0

是的,按照我在回答中提供的內容併發布任何/所有錯誤消息,你會得到。使'AyncTask'成爲你的'MainActivity'的一個內部類,然後把'directions'的內容放在'doInBackground()'中,或者簡單地從'do'inBackground()'中調用'directions'並且設置'directions' a方法在你的'AsyncTask類' – codeMagic 2013-03-13 12:49:58

0

這裏是什麼,我認爲他們的意思時,他們說,你可以使用的AsyncTask:

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 
    RouteTask routeTask = new RouteTask(new GeoPoint((int)(26.2*1E6),(int)(50.6*1E6)), new GeoPoint((int)(26.3*1E6),(int)(50.7*1E6)); 
    routeTask.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; 
} 

class RouteTask extends AsyncTask<String, Void, String> { 
    private GeoPoint start; 
    private GeoPoint dest; 
    private Route route; 

    public RouteTask(GeoPoint start, GeoPoint dest) { 
     this.start = start; 
     this.dest = dest; 
    } 

    protected String doInBackground(String... strings) { 
     //runs the directions method in a background thread. 
     route = directions(start, dest); 
     return "Started"; 
    } 

    @Override 
    protected void onPostExecute(String s) { 
     //Updates the UI on the main/UI thread (post execute is called on the main/UI thread). 
     RouteOverlay routeOverlay = new RouteOverlay(route, Color.BLUE); 
     mapView.getOverlays().add(routeOverlay); 
     mapView.invalidate(); 
    } 
} 

}

這將運行在後臺線程的方向的方法和更新主/ UI線程在地圖上。作爲所有編程語言的一般練習,您應該在繪製到屏幕的線程(本例中爲主/ UI線程)上執行UI工作。

這應該做你所需要的。

+0

謝謝。我使用下面的鏈接在Android的駕駛方向,他們說只是在asynctask中使用direction()函數,但仍然不能停止應用程序..可以幫助我。 http ://stackoverflow.com/questions/11745314/why-retrieving-google-directions-for-android-using-kml-data-is-not-working-anymo/11745316#11745316 – 2013-03-13 05:47:11

+0

我更新了我的答案,讓我知道如果那爲你工作。 – 2013-03-13 13:13:31