我想通過使用處理程序每隔1分鐘運行一次AsyncTask,但它不起作用:( 在AsyncTask中我正在從Sqlite數據庫讀取數據並在mapview中繪製結果,這樣做的目的是另一項服務更新我的SQLite的每1分鐘由服務器以實際的數據,我想保留的MapView更新過。 是否有打電話的AsyncTask每分鐘,而不是使用處理器的任何可能的方式?如何每分鐘運行一次AsyncTask?
public Runnable getData;
private final Handler _handler = new Handler();
private static int DATA_INTERVAL = 60 * 1000;
getData = new Runnable()
{
public void run()
{
getDataFrame();
}
private void getDataFrame() {
// TODO Auto-generated method stub
_handler.postDelayed(MapViewActivity.this.getData, DATA_INTERVAL);
new DrawFromDataTask();
}
};
DrawFromDataTask is described below:
private class DrawFromDataTask extends AsyncTask<Void, Integer, FriendItemizedOverlay> {
@Override
protected FriendItemizedOverlay doInBackground(Void... params) {
// TODO Auto-generated method stub
mDbAdapter.open();
List<Refresher> nearFriends = mDbAdapter.getAllRecords();
for(Refresher friend : nearFriends)
{
double lat = Double.parseDouble(friend.Latitude);
double lon = Double.parseDouble(friend.Longitude);
OverlayItem item = new OverlayItem(new GeoPoint((int)(lat * 1000000),
(int)(lon* 1000000)),
"" + friend.name,
"" + friend.type);
mFriendOverlay.addOverlay(item);
}
return mFriendOverlay;
}
protected void onProgressUpdate(Integer... progress) {
setProgress(progress[0]);
}
protected void onPostExecute(FriendItemizedOverlay result) {
System.out.println("in AsyncTask execution!");
Location loc = get_location();
final double mLatitude = loc.getLatitude();
final double mLongitude = loc.getLongitude();
// get the last location from the database
GeoPoint lastLocation = new GeoPoint(
(int) (mLatitude * 1E6),
(int) (mLongitude * 1E6));
Drawable marker = getResources().getDrawable(R.drawable.arrow);
int amountOFplayers = result.size()-1;
for (int j=0; j<amountOFplayers; j++) {
result.getItem(amountOFplayers).setMarker(marker);
}
//System.out.println("Number of overlays -- "+amountOFplayers);
mMapView.postInvalidate();
//mMapView.invalidate();
// animate to last location
mMapController.animateTo(lastLocation);
// zoom to the required level
mMapController.setZoom(ZOOM_LEVEL);
}
}
你是什麼意思「它不工作」。添加一些調試日誌記錄,看看發生了什麼。你有沒有發佈'getData' Runnable?它只運行一次嗎?它是否將任何錯誤寫入logcat? – 2012-07-16 16:12:10
沒有錯誤,它只運行一次。 – 2012-07-16 16:42:51
AsyncTask只運行一次?或者getData Runnable只運行一次? – 2012-07-16 17:11:33