朋友,如果长时间运行的循环正在工作,AsyncTask不会停止?
我用下面的代码里面的AsyncTask
公共类AsycLoaderFromDbAndMapInjector扩展的AsyncTask {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Boolean doInBackground(Void... arg0) {
Log.d("Asynctask", ""+arg0);
boolean show = false;
if(db == null)
db = new dbHelper(me);
ATM results = null;
try {
results = db.getAtmsBySelectedBanks(atm.getSelectedBankList(), selectedCity);
if (results != null) {
LoopThroughEachATMToDisplayOnMap();
}
return show;
}
@Override
protected void onPostExecute(Boolean result) {
stopProgress();
}
私人无效LoopThroughEachATMToDisplayOnMap() {
Drawable drawable = getResources().getDrawable(
R.drawable.marker);
itemizedOverlay = new MyItemizedOverlay(drawable, mapView);
for (int i = 0; i < atm.getAtmList().size(); i++) {
ATM a = atm.getAtmList().get(i);
if (a != null) {
int[] coordinates = getIntCoordinates(a
.getCoordinates());
if (coordinates != null) {
GeoPoint point = new GeoPoint(coordinates[0],
coordinates[1]);
OverlayItem overlayItem = new OverlayItem(
point, a.getBankName(), a.getAddress()
+ "@@" + a.getPhone() + "@@"
+ a.getWebAddress() + "@@"
+ a.getCoordinates());
itemizedOverlay.addOverlay(overlayItem);
System.out
.println("coordinates-------------------------"
+ i + " "
+ coordinates[0]
+ ","
+ coordinates[1]);
}
}
}
}
}
现在,当我按下这个可运行的按钮填充继续在后端运行我已经使用
AsyncTask.cancel()在backbuttonpressed事件,但它仍然运行任何人指导我如何解决这个问题?
根据AsyncTask文档,onPostExecute在UI线程上执行。从它调用runOnUiThread将导致立即执行填充,因此看起来很难阻止它。 取消AsyncTask将不会取消onPostExecute执行,因为它由UI线程执行。 将消息发送到onPostExecute上的Handler可能会更好,或延迟可运行的执行。 – david 2011-06-03 10:32:01
你可以发布整个异步任务吗? – Egor 2011-06-03 10:33:20
您不需要在UI线程上运行它 – Hades 2011-06-03 10:35:32